shell中下列符号的含义
$0 就是该bash文件名 $? 是上一指令的返回值 $* 所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。 $@ 所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。 “$* ”返回的是一个字符串,字符串中存在多外空格。 “$@ ”返回多个字符串。
#单行注释 :<<BLOCK ....被注释的多行内容 如果被注释的内容中有反引号会报错 BLOCK :<< 'BLOCK ....被注释的多行内容 解决注释中有反引号的问题 BLOCK' :<< ' ....被注释的多行内容 ' :||{ ....被注释的多行内容 当注释内容中有括号时报语法错误错,但里面有反引号,引号时没有问题 } if false ; then ....被注释的多行内容 会对注释内容中的括号引号等语法错误报错 fi
#批量替换文件内容 方法一:
new_name="MGL" old_name="QGL" find ./ -name "*.c" -exec sed -i "s/$old_name/$new_name/g" '{}' \;
方法二:
sed -i s/"str1"/"str2"/g `grep "str1" -rl --include="*.[ch]" ./`
sed -i s/"str1"/"str2"/g `grep "str1" -rl --include=*.{cpp,h} ./`
#输出将文本中orginal字符串替换为target awk '{sub(/original/,"target");print}' txt
#批量修改文件名 rename 's/qgl/mgl/' lib/*
#替换内容并修改文件名 old_str="old" new_str="new" for f in ./*/*.{cpp,h}; do echo $f sed -i "s/$old_str/$new_str/g" $f mv $f `echo $f | sed -e "s/$old_str/$new_str/g"` done
# 读取ini类型文件参数值 source local.conf echo $tablename
#目录 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 if [ ! -x "$myPath"]; then mkdir "$myPath" fi #这里的-d 参数判断$myPath是否存在 if [ ! -d "$myPath"]; then mkdir "$myPath" fi #这里的-f参数判断$myFile是否存在 if [ ! -f "$myFile" ]; then touch "$myFile" fi #其他参数还有-n,-n是判断一个变量是否是否有值 if [ ! -n "$myVar" ]; then echo "$myVar is empty" exit 0 fi
后台执行,如我想在shell脚本中, a,b,c 三个命令,并行执行。但是d命令,同时依赖a,b,c的结果。那么我们可以如下执行shell脚本
#!/usr/bin/sh a & b & c & wait d
远程执行命令自动输入密码
#!/bin/sh ip=$1 passwd=$2 expect -c " spawn ssh admin@$ip \"pwd\" expect { \"*assword\" {set timeout 300; send \"$passwd\n\";} } expect eof"