贡献者: addis
.sh 的文件,也可以没有.
./name.sh 也可以用 source /path/name.sh, 前者在一个新的 shell process 中运行, 后者在当前的 shell process 运行, 应该是相当于直接把文件中的内容复制粘贴到命令行执行.
> 把 shell 中的 stdout 导入文件.
#! 指定运行使用的 shell,如 #!/bin/bash
exit 结束 script,exit 0 声明运行成功
# 注释
var=xxx,等号两边不能有空格.$var 获取它的值
$(命令) 或者 `命令` 相当于把 命令 的输出展开到当前位置,如 echo current dir: `pwd -P`
bins="123 234"
exec 文件名.sh 执行另一个脚本
echo 123;
if [ $? -ne 0 ]; then # 判断 exit status
...
fi
#!/bin/bash
for ((i = 1; i < 10; ++i)) # for i in {1,2,3,4}
do
printf "\nfile${i}.txt\n"
done
#!/bin/bash
arr=(1 3 hello 13)
for ((i=0;i<4;++i))
do
printf "arr[${i}] = ${arr[i]}\n"
done
例子(确保文件最后由两个空格)
#!/bin/bash
# use `find . -type f -name "*.matt" -exec ./convert_matt.sh {} \;`
# to convert subfolder
file=${1}
printf "${file}... "
c2=$(tail -c 2 ${file})
if [ "${c2}" == " " ]
then
printf "already in new format\n"
else
printf " " >> ${file}
printf "done\n"
fi
$0 当前 shell script 的文件名
$@ 所有 shell arguments
$? 上一个命令的 exit status,0 代表成功,否则失败
函数名() {
...
}
$1, $2,例如
greeting () {
echo "Hello $1"
}
greeting "Joe"
 
 
 
 
 
 
 
 
 
 
 
友情链接: 超理论坛 | ©小时科技 保留一切权利