shell脚本基础改造
一、基础的shell脚本格式
#!/bin/bash 2 #3 #********************************************************************4 #Author: LJH5 #QQ: 20435658306 #Date: 2024-03-21 10:09:037 #FileName: if.sh8 #URL: https://blog.csdn.net/cnluxiansheng?spm=1000.2115.3001.53439 #Description: For personal learning Bash10 #Copyright (C): 2024 All rights reserved11 #********************************************************************12 13 14 if [];then #括号中添加空格输出结果为空。15 echo16 fi
输出结果:
[root@kvm-72 sh]# sh if01.sh
if01.sh:行14: []:未找到命令
二、shell脚本输出括号中的passwd。
1 #!/bin/bash2 #3 #********************************************************************4 #Author: LJH5 #QQ: 20435658306 #Date: 2024-03-21 10:15:347 #FileName: if02.sh8 #URL: https://blog.csdn.net/cnluxiansheng?spm=1000.2115.3001.53439 #Description: For personal learning Bash10 #Copyright (C): 2024 All rights reserved11 #********************************************************************12 13 14 if [ -f /etc/passwd ] ;then 16 echo 'passwd!'17 fi
输出结果:
[root@kvm-72 sh]# sh if02.sh
passwd!
[root@kvm-72 sh]#
三、
#!/bin/bash2 #3 #********************************************************************4 #Author: LJH5 #QQ: 20435658306 #Date: 2024-03-21 10:33:557 #FileName: if03-JudgeStr.sh8 #URL: https://blog.csdn.net/cnluxiansheng?spm=1000.2115.3001.53439 #Description: For personal learning Bash10 #Copyright (C): 2024 All rights reserved11 #********************************************************************12 13 if [ "guomc" ];then14 echo "guomc str is exits!!!"15 16 fi
输出结果:
[root@kvm-72 sh]# sh if03-JudgeStr.sh
guomc str is exits!!!
四、判断目录是否存在,存在即退出不存在则创建后确认存在退出。
1 #!/bin/bash2 #3 #********************************************************************4 #Author: LJH5 #QQ: 20435658306 #Date: 2024-03-21 10:33:557 #FileName: if03-JudgeStr.sh8 #URL: https://blog.csdn.net/cnluxiansheng?spm=1000.2115.3001.53439 #Description: For personal learning Bash10 #Copyright (C): 2024 All rights reserved11 #********************************************************************12 mydir='/data/mysql/mysql3316'13 if [ -d "${mydir}" ];then14 /usr/bin/echo "${mysdir} is exit!!!"15 exit16 17 else18 /usr/bin/mkdir -pv ${mydir}19 [ -d ${mydir} ] && echo ${mydir} is exits!!!20 echo "${mydir} was by created!!!"21 exit 322 fi
输出结果:显示存在并退出
[root@kvm-72 sh]# sh if04-JudgeStrV2.sh is exit!!!
[root@kvm-72 sh]#
五、两组数字666 888判断是否相等
1 #!/bin/bash2 #3 #********************************************************************4 #Author: LJH5 #QQ: 20435658306 #Date: 2024-03-21 10:57:087 #FileName: if05-JudgeNum.sh8 #URL: https://blog.csdn.net/cnluxiansheng?spm=1000.2115.3001.53439 #Description: For personal learning Bash10 #Copyright (C): 2024 All rights reserved11 #********************************************************************12 13 14 num1=66615 num2=88816 if [ ${num1} -eq ${num2} ] ; then17 echo "${num1} equal ${num2}" 18 exit 019 20 else21 echo "${num1} not equal ${num2}"22 exit 6 23 24 fi
输出结果:666不等于888
[root@kvm-72 sh]# sh if05-JudgeNum.sh
666 not equal 888
六、上一个脚本的升级版
1 #!/bin/bash2 #3 #********************************************************************4 #Author: LJH5 #QQ: 20435658306 #Date: 2024-03-21 10:57:087 #FileName: if05-JudgeNum.sh8 #URL: https://blog.csdn.net/cnluxiansheng?spm=1000.2115.3001.53439 #Description: For personal learning Bash10 #Copyright (C): 2024 All rights reserved11 #********************************************************************12 13 14 num1=66615 num2=88816 if [[ ${num1} > ${num2} ]] ; then #添加了一对函数 # 17 echo "${num1} > ${num2}" 18 exit 019 20 else21 echo "${num1} < ${num2}"22 exit 623 24 fi
输出结果:666小于888
[root@kvm-72 sh]# sh if06-JudgeNumV2.sh
666 < 888
七、python脚本,大于等于18岁可以进入网咖
age = 184 if age >= 18:5 #if [ ] ; then6 print("恭喜,您可以进入网咖了...")
输出结果:判断18岁可以进入网咖
[root@kvm-72 py]# python if01.py
恭喜,您可以进入网咖了...
[root@kvm-72 py]#
八、未成年不可以进入网咖
3 age = 10 4 if age < 18:5 #if [ ] ; then6 print("No, 未成年不可以进入网咖")7 else:8 print("yse,成年可以进入网咖")
输出结果:
print("No, 未成年不可以进入网咖")7 else:8 print("yse,成年可以进入网咖")
九、python脚本的基本输出方式
6 name = '郭'7 age = 208 address = '原怡丰'9 10 print("我的名字是:", name)11 print("我的年龄是:", age + 1) 12 print("我的住址是:", address)
输出结果:
[root@kvm-72 py]# python var01.py
我的名字是: 郭
我的年龄是: 21
我的住址是: 原怡丰