网站管理平台扩展插件,网站建设基本流程是什么,计算机上网题的模拟网站怎么做,企业查查天眼查询优质博文#xff1a;IT-BLOG-CN
【1】x.sh文件内容编写#xff1a; 固定开头#xff1a;##xff01;/bin/sh#xff1b; 【2】学习的第一个命令就是echo输出的意思#xff1b; 【3】其实shell脚本也就是在文件中写命令#xff0c;但是我们要写的是绝对路径#xff1a…优质博文IT-BLOG-CN
【1】x.sh文件内容编写 固定开头#/bin/sh 【2】学习的第一个命令就是echo输出的意思 【3】其实shell脚本也就是在文件中写命令但是我们要写的是绝对路径eg/bin/pwd 【4】运行shell脚本sh文件名 【5】通过下面脚本进行学习
#!/bin/sh
/bin/date %F /test/shelldir/ex2.info #data %F是将日期格式化。追加输出
echo disk info: /test/shelldir/ex2.info
/bin/df -h /test/shelldir/ex2.info
echo /test/shelldir/ex2.info
echo online users: /test/shelldir/ex2.info
/usr/bin/who | /bin/grep -v root /test/shelldir/ex2.info #使用的命令主要来自两个地方①、/bin/ ②、/usr/bin/ -v:表示排除
echo memory info: /test/shelldir/ex2.info
/usr/bin/free -m /test/shelldir/ex2.info
echo /test/shelldir/ex2.info
#write root
/usr/bin/write root /test/shelldir/ex2.info /bin/rm /test/shelldir/ex2.info
crontab -e #定时执行命令
0 9 * * 1-5 /bin /sh /test/ex2.sh #表示每周一到周五的9点执行该脚本。【6】变量 是shell传递数据的一种方式用来代表每个取值的符号。 Shell的变量有两种①、永久变量 ②、临时变量。临时变量是shell程序内部定义的其使用范围只限于定义它的程序其它程序不可见。包括程序自定义变量、位置变量。永久变量是环境变量。其值不随 shell脚本的执行结束而消失。
【永久变量】
【自定义变量】 在使用变量时要在变量前面加前缀$一般变量使用大写字母表示并且是英文字母开头赋值号“”两边没有空格如NUM5、STRA string。可以将一个命令的执行结果赋值给一个变量但需要使用命令替换符。NUMdata注意单引号和双引号之间的区别“”是会将里面的变量值进行输出‘’会将里面的内容原封不动的进行输出不会识别里面的变量。使用set查看所有变量。查询$变量。使用unset命令删除指定的变量。
【7】占位变量 在shell中还有两种常用的变量一种是占位变量还有一种是特殊变量在编写shell到的时候特别常用
【位置变量】 ls -l file1 file2 file3...n范围1-9在代码里使用$0-9进行替换。也就是说查看file1目录地下的所有文件信息。
#!/bin/sh
DATE/bin/date %Y%m%d
echo TODAY IS $DATE
/bin/ls -l $1
/bin/ls -l $2
/bin/ls -l $3在命令行输入sh文件名/test /usr/bin /home。解释就是让命令中的/test替换脚本中的$1........
【8】特殊变量 ■ $*这个程序的所有参数 ■ $#这个程序的参数个数 ■ $$这个程序的PID ■ $! 执行上一个程序的PID ■ $?执行上一个命令的返回值 ■ $0-9显示位置变量
【9】read键盘输入命令read从键盘读取数据赋给变量。
#!/bin/sh
read f s t
echo the first is $f
echo the second is $s
echo the third is $t执行命令 sh文件名先执行在输入变量。如果输入sh -x文件名执行 shell脚本当执行到read时会弹出read f s t然后我们根据需求输入例如10 20 30
【10】shell的运算 expr命令对整数进行运算。注意点 ①、expr的计算必须用空格隔开 ②、\*表示转义字符 ③、保持先算乘除后算加减如果需要优先算法需要加命令替换符 ④、可以对变量进行运算 【11】test测试命令 使用test命令可以对文件、字符串等进行测试一般配合控制语句使用不应该单独使用。 【12】if语句语法格式
if [ -d $1 ]
then
else
fi【实例展示】
#!/bin/sh
# if test $1 then ... else ... fi
if [ -d $1 ]
thenecho this is a directory!
elseecho this is not a directory!
fi【if elif语法】
#!/bin/sh
# if test then ... elif test then ... else ... fi
if [ -d $1 ]
thenecho is a directory!
elif [ -f $1 ]thenecho is a file!
elseecho error!
fi【逻辑 与-a和 或-o】
#!/bin/sh
# -a -o
if [ $1 -eq $2 -a $1 1 ]thenecho param1 param2 and param1 1
elif [ $1 -ne $2 -o $1 2 ]thenecho param1 ! param2 or param1 2
elseecho others
fi【for循环】
#!/bin/sh
# for var in [params] do ... done
for var in 1 2 3 4 5 6 7 8 9 10
do echo number is $var
done【select循环】
#!/bin/sh
# select var in [params] do ... done
select var in java c php linux python ruby c#
dobreak
done
echo you selected $var【case循环】
#!/bin/sh
read op
case $op ina)echo you selected a;;b)echo you selected b;;c)echo you selected c;;*)echo error
esac【while循环】
#!/bin/sh
#while test do ... donenum1
sum0
while [ $num -le 100 ] #le表示小于等于
dosumexpr $sum $numnumexpr $num 1
done
#sleep 5
echo $sum