Window PowerShell 自学指南2


这一章是听课记录。是个人理解,有差正常。

1.赋值=  与单双引号的差别

2. vim 建立函数

比如新建一个mcd.sh的函数

vim mcd.sh进入vim 界面

然后按i进入Insert模式,只能插入,不能修改

然后编辑函数:

mcd () {
    mkdir -p "$1"
    cd "$1"
}

比如你上述发现某一个写错了 ,你需要按【esc】退出到命令行模式再修改。

现在你需要保存这个函数,在命令行模式下按【:】返回最后一行,然后输入【w filename】保存。

接下来退出vim,在命令行模式下输【:wq】,意思是返回最后一行,written and quit

 现在使用这个函数,这个函数的意思是建立一个dir并且进入该dir

比如 mcd test,输出为

3. $的使用

  4.输入输出

 这表示第一条代码输出成功,为0。

 由于mcd.sh里并没有foobar,所以输出失败,为1。

5.true ,false 和 ||,&&

||是咋输出的,咋交换位置输出还不一样,看不懂

 同理&&也看不懂

6.

cat <(ls)

 cat <(ls) <(ls..)

这个得注意空格的位置。

7.写一个函数样例

见2

vim example.sh

i进入Insert

写函数

#!/bin/bash

echo "Starting program at $(date)" # Date will be substituted

echo "Running program $0 with $# arguments with pid $$"

for file in "$@"; do
    grep foobar "$file" > /dev/null 2> /dev/null
    # When pattern is not found, grep has exit status 1
    # We redirect STDOUT and STDERR to a null register since we do not care about them
    if [[ $? -ne 0 ]]; then
        echo "File $file does not have any foobar, adding one"
        echo "# foobar" >> "$file"
    fi
done

退到命令行,保存。

运行上述代码:

7.查看当前路径下特定类型的文件

 同理查看prj1 prj2 prj3可以通过,ls prj?

转换文件格式:
convert image.png image.jpg

7.批量操作

批量创建

8.写一个python函数

 vim script.py

#!/usr/local/bin/python

import sys
for arg in reversed(sys.argv[1:]):
    print(arg)
如果你有python 编译器的话,可以执行如果代码,当然我卸载了,所以结果如图

9.shell functions和scripts的区别

我之前写的应该不是叫函数,叫script

1.function只能与shell相同的语言写,script可以用不同的,见8。

2.function在定义后加载,script要先执行再加载。

3.function在shell环境中执行,script在它自己的环境中执行。

4.....

10.find 指令的使用

这个代码写的很清楚了

# Find all directories named src
find . -name src -type d
# Find all python files that have a folder named test in their path
find . -path '*/test/*.py' -type f
# Find all files modified in the last day
find . -mtime -1
# Find all zip files with size in range 500k to 10M
find . -size +500k -size -10M -name '*.tar.gz'
# Delete all files with .tmp extension
find . -name '*.tmp' -exec rm {} \;
# Find all PNG files and convert them to JPG
find . -name '*.png' -exec convert {} {}.jpg \;



10.grep 指令的使用