linux就该这么学第四章之vim编译器与shell命令脚本

Vim文本编译器

[linuxprobe@linuxprobe Desktop]$ vim example.sh
[linuxprobe@linuxprobe Desktop]$ sh example.sh one two three four five six   ##后面均为输入SHELL的参数,分别为第一第二...个参数
the txt name is example.sh
it have 6 parameters , they are one two three four five six
the first parameter is one,the fifth parameter is five
[linuxprobe@linuxprobe Desktop]$ 

example里面的内容

#!/bin/bash
echo "the txt name is $0"      ##$0对应文本的名称  $1$2对应第一第二个参数
echo "it have $# parameters , they are $*"     ###$*显示所有参数
echo "the first parameter is $1,the fifth parameter is $5"
[linuxprobe@linuxprobe Desktop]$ [ -d /etc/fstab ]          ##空格要空好,不然会失败
[linuxprobe@linuxprobe Desktop]$ echo $?
1                                                                                       ##目录不存在

当前用户是linuxprobe,,不是root

[linuxprobe@linuxprobe Desktop]$ echo $USER
linuxprobe
[linuxprobe@linuxprobe Desktop]$ echo "user"
user
[linuxprobe@linuxprobe Desktop]$ [ $USER = root ] || echo "user"
user
[linuxprobe@linuxprobe Desktop]$ [ $USER = linuxprobe ] || echo "user"
[linuxprobe@linuxprobe Desktop]$ 

比较数字大小,参数见84

[linuxprobe@linuxprobe Desktop]$ [ 10 -gt 10 ]
[linuxprobe@linuxprobe Desktop]$ echo $?
1
[linuxprobe@linuxprobe Desktop]$ [ 10 -eq 10 ]
[linuxprobe@linuxprobe Desktop]$ echo $?
0
[linuxprobe@linuxprobe asdf]$ vim asd.ch
[linuxprobe@linuxprobe asdf]$ bash asd.ch             ## **执行文档内容**
Enter your score (0-100):75
75 is Pass

文档的内容

#!/bin/bash 
read -p "Enter your score (0-100):" GRADE
if [ $GRADE -ge 85 ] && [ $GRADE -le 100 ];then 
echo "$GRADE is Excellent"
elif [ $GRADE -ge 70 ] && [ $GRADE -le 84 ] ;then 
echo "$GRADE is Pass"
else
echo "$GRADE is Fail"
fi
#!/bin/bash
read -p "Enter The User Password:" PASSWD
for UNAME in `cat asd.txt`                ##这里是反引号
do
id $UNAME &> /dev/null
if [ $? -eq 0 ]
then
echo "Already exists"
else
useradd $UNAME &> /dev/null
echo "$PASSWD" | passwd --stdin $UNAME &> /dev/null
if [ $? -eq 0 ]
then
echo "$UNAME ,Create success"
else
echo "$UNAME ,Create failure"
fi
fi
done
[root@linuxprobe Desktop]# bash creatuser.sh
Enter The User Password:xuzedu
qwe ,Create success
rty ,Create success
uio ,Create success
asd ,Create success
fgh ,Create success
jkl ,Create success
zxc ,Create success
vbn ,Create success
[root@linuxprobe Desktop]# tail -6 /etc/passwd
uio:x:1003:1003::/home/uio:/bin/bash
asd:x:1004:1004::/home/asd:/bin/bash
fgh:x:1005:1005::/home/fgh:/bin/bash
jkl:x:1006:1006::/home/jkl:/bin/bash
zxc:x:1007:1007::/home/zxc:/bin/bash
vbn:x:1008:1008::/home/vbn:/bin/bash

刚开始可能因为没有获得root权限一直失败,获得权限后成功

这是因为在shell脚本中等号两边不能有空余的空格(可能我们在其他变成语言中形成了一个习惯,等号两边总要留个空格使代码美化)不过符号两边需要空格

expr的作用:对后面的式子进行计算并将结果赋给前面的变量

[root@linuxprobe Desktop]# ASD=456%120
[root@linuxprobe Desktop]# echo $ASD
456%120
[root@linuxprobe Desktop]# ASD=expr 456%120
bash: 456%120: command not found...
[root@linuxprobe Desktop]# ASD=(expr 456%120)
[root@linuxprobe Desktop]# echo $ASD
expr
[root@linuxprobe Desktop]# ASD=$(expr 456%120)         //求余符号两边空格后才能计算
[root@linuxprobe Desktop]# echo $ASD
456%120
[root@linuxprobe Desktop]# ASD=$(expr 456 % 120)
[root@linuxprobe Desktop]# echo $ASD
96

while循环的例子
注意:等号两边不能有空格,计算符号两边需要空格

#!/bin/bash
PRICE=$(expr $RANDOM % 1000)
TOMES=0
while true 
do
read -p "inter the 0~999:" INT
let TIMES++
if [ $INT -eq $PRICE ];then
echo "congretulation"
echo "TIMES is $TIMES"
exit 0
elif [ $INT -gt $PRICE ]; then 
echo "it is too high"
else 
echo "it is too low"
fi
done
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值