$ {}
$表示取值 {}表示代码边界,帮助解释器识别变量的边界 或{ }表示代码块 相当于创建了一个匿名函数 对脚本的其他部分是可见的
$PATH:/home/shiyanlou
${PATH}:/home/shiyanlou"
直接用$表示取其后面所有的
dirname "$0"
当前路径
传入外部参数
info.sh写法为
name = $1//错误 等号两侧不能留空格 否则会包commond not found
name = $1
age = $2
echo " ${name}的年龄是${age}"
对应执行情况为
bash info.sh xiaoming 20
如果用./执行可能会有permission deny 所以用bash执行
for循环
#!/bin/bash
num=$1
for ((i=1; i<=num; i++))
do
touch file$i && echo file $i is ok
done
for str in This is a string
do
echo $str
done
This
is
a
string
if
num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo 'Two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
case
#!/bin/bash
varname=b
case "$varname" in
[a-z] echo "abc";;
[0-9] echo "123";;
esac
输出PATH
echo PATH或echo 'PATH’或echo “PATH”
输出PATH变量的内容
echo $PATH
脚本的执行
①在脚本所在目录下
使用.来执行
./hello.sh
使用shell来执行
sh hello.sh
使用bash来执行
bash hello.sh
使用source来执行
source hello.sh
②在任意目录下 ./hello.sh
需要将文件所在的绝对路径加到PATH环境变量里;PATH是个存各种可执行文件的路径的环境变量文件,路径之间用:隔开;用命令: echo “PATH=$PATH:/home/shiyanlou” >> .zshrc添加
使用;可以在同一行写两个或两个以上命令
//运行python文件
python dunp.py
//python文件程序中有sys.argv[1] sys.argv[2] 运行python文件时传入参数
python dump.py arg1 arg2
//运行python文件 输出到result.txt文件
python dump.py > result.txt