简单shell脚本参考

eg1:

if [ $# -ne 2 ]
then
echo "Usage: $0 dirname filename"
exit 1
fi
#---------------------------
echo $#
#---------------------------
echo $@
#---------------------------
echo $0
#---------------------------
mkdir $1
cd $1
touch $2.txt

如果输入的变量不等于2

显示"Usage: $0(sh的名字 dirname filename"
退出 返回值1

------------------------------注释

显示输入的变量的总数
#------------------------------注释
显示输入的所变量
#------------------------------注释
显示当前执行sh的名字

建立文件夹 名称为输入的第一个变量

跳转到该文件夹

建立文件 名称为输入的第二个变量

eg2:
AAA='hello' --定义变量AAA字符串,值为‘hello'
BBB='world' --定义变量BBB字符串,值为’world‘

echo $AAA $BBB --打印两个变量到屏幕

exit 0 --注释,退出,返回值0

Tmp='
111
222
333
444
555' --定义tmp

for i in $Tmp --循环读取Tmp放到i中
do
if [ $i -eq 111 ] --如果值不为111
then
echo $i --打印
fi
done

eg3:
while read line --循环读入link
do
echo $line --打印link
done < 1.txt --取1.txt中的数据

Tmp=cat 1.txt --使用命令cat读取1.txt 注意`非'

for i in $Tmp
do
echo $i
done

eg4:
while read line --循环读取line
do
echo $line > tmp.txt --显示tmp.txt
echo "load from tmp.txt insert into tlstellerinfo" | dbaccess lndep > /dev/null 2>&1 --load tmp.txt到表中
if [ $? -ne 0 ] --判断返回值是否为0 即是否执行成功
then
echo $line >> error.txt --将错误信息逐行打印到error.txt
fi
done < teller.txt

posted @ 2020-10-27 21:27  江山此夜寒  阅读(129)  评论(0)    收藏  举报