ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

SHELL学习笔记二

2019-10-24 15:01:57  阅读:234  来源: 互联网

标签:SHELL testuser 检查 笔记 echo 学习 sh file fi


SHELL学习笔记二

 使用 if-then 语句
 嵌套 if 语句  
 test 命令
 复合条件测试
 使用双方括号和双括号
 case 命令

 

 使用 if-then 语句

if command
then
    commands
fi 
$ cat test1.sh 
#!/bin/bash 
# testing the if statement 
if pwd 
then 
    echo "It worked" 
fi 
$ 

$ ./test1.sh 
/home/Christine 
It worked 
$ 

 if-then-else 语句

if command 
then 
   commands 
else 
   commands 
fi 
$ cp test3.sh test4.sh 
$ 
$ nano test4.sh 
$ 
$ cat test4.sh 
#!/bin/bash 
# testing the else section 
# 
testuser=NoSuchUser 
# 
if grep $testuser /etc/passwd 
then 
   echo "The bash files for user $testuser are:" 
   ls -a /home/$testuser/.b* 
   echo 
else 
   echo "The user $testuser does not exist on this system." 
   echo 
fi 
$ 
$ ./test4.sh 
The user NoSuchUser does not exist on this system. 
$ 

嵌套 if 

if command1 
then 
   commands 
elif command2 
then 
    more commands 
fi 
$ cat test5.sh 
#!/bin/bash 
# Testing nested ifs - use elif 
# 
testuser=NoSuchUser 
# 

if grep $testuser /etc/passwd 
then 
   echo "The user $testuser exists on this system." 
# 
elif ls -d /home/$testuser 
then 
   echo "The user $testuser does not exist on this system." 
   echo "However, $testuser has a directory." 
# 
fi 
$ 
$ ./test5.sh 
/home/NoSuchUser 
The user NoSuchUser does not exist on this system. 
However, NoSuchUser has a directory. 
$ 

 test 命令

if test condition 
then 
   commands 
fi 
$ cat test6.sh 
#!/bin/bash 
# Testing the test command 
# 
if test 
then 
   echo "No expression returns a True" 
else 
   echo "No expression returns a False" 
fi 
$ 
$ ./test6.sh 
No expression returns a False 
$ 
#可以使用 test 命令确定变量中是否有内容。这只需要一个简单的条件表达式。 
$ cat test6.sh 
#!/bin/bash 
# Testing the test command 
# 
my_variable="Full" 
# 
if test $my_variable 
then 
   echo "The $my_variable expression returns a True" 
# 
else 
   echo "The $my_variable expression returns a False" 
fi 
$ 
$ ./test6.sh 
The Full expression returns a True 
$ 
#变量 my_variable 中包含有内容( Full ),因此当 test 命令测试条件时,返回的退出状态为 0 。这使得 then 语句块中的语句得以执行。 

  数值比较

  •  n1 -eq n2  检查n1是否与n2相等
  • n1 -ge n2  检查n1是否大于或等于n2  
  • n1 -gt n2  检查n1是否大于n2  
  • n1 -le n2  检查n1是否小于或等于n2  
  • n1 -lt n2  检查n1是否小于n2  
  • n1 -ne n2  检查n1是否不等于n2 
$ cat numeric_test.sh 
#!/bin/bash 
# Using numeric test evaluations 
# 
value1=10 
value2=11 
# 
if [ $value1 -gt 5 ] 
then 
    echo "The test value $value1 is greater than 5" 
fi 
# 
if [ $value1 -eq $value2 ] 
then 
    echo "The values are equal" 
else 
    echo "The values are different" 
fi 
# 
$ 

字符串比较

  • str1 = str2  检查str1是否和str2相同
  • str1 != str2  检查str1是否和str2不同
  • str1 < str2  检查str1是否比str2小
  • str1 > str2  检查str1是否比str2大
  • -n str1  检查str1的长度是否非0
  • -z str1  检查str1的长度是否为0
#字符串不等条件也可以判断两个字符串是否有相同的值。 
$ cat test8.sh 
#!/bin/bash 
# testing string equality 
testuser=baduser 
# 
if [ $USER != $testuser ] 
then 
   echo "This is not $testuser" 
else 
   echo "Welcome $testuser" 
fi 
$  
$ ./test8.sh 
This is not baduser 
$ 

字符串顺序

要测试一个字符串是否比另一个字符串大就是麻烦的开始。当要开始使用测试条件的大于或
小于功能时,就会出现两个经常困扰shell程序员的问题:  

  •  大于号和小于号必须转义,否则shell会把它们当作重定向符号,把字符串值当作文件名;
  •  大于和小于顺序和 sort 命令所采用的不同。

字符串大小
-n 和 -z 可以检查一个变量是否含有数据。

文件比较

  • -d file  检查file是否存在并是一个目录
  • -e file  检查file是否存在
  • -f file  检查file是否存在并是一个文件
  • -r file  检查file是否存在并可读
  • -s file  检查file是否存在并非空
  • -w file  检查file是否存在并可写
  • -x file  检查file是否存在并可执行
  • -O file  检查file是否存在并属当前用户所有
  • -G file  检查file是否存在并且默认组与当前用户相同
  • file1 -nt file2  检查file1是否比file2新
  • file1 -ot file2  检查file1是否比file2旧

  复合条件测试

if-then 语句允许你使用布尔逻辑来组合测试。有两种布尔运算符可用:

  •  [ condition1 ] && [ condition2 ]
  •  [ condition1 ] || [ condition2 ] 

 if-then 的高级特性

bash shell提供了两项可在 if-then 语句中使用的高级特性:

  •  用于数学表达式的双括号 (( expression ))
  •  用于高级字符串处理功能的双方括号 [[ expression ]]

 双括号命令符号

  • val++  后增
  • val--  后减
  • ++val  先增
  • --val  先减
  • !  逻辑求反
  • ~  位求反
  • **  幂运算
  • <<  左位移
  • >>  右位移
  • &  位布尔和
  • |  位布尔或
  • &&  逻辑和
  • ||  逻辑或
$ cat test23.sh 
#!/bin/bash 
# using double parenthesis 
# 
val1=10 
# 
if (( $val1 ** 2 > 90 )) 
then 
   (( val2 = $val1 ** 2 )) 
   echo "The square of $val1 is $val2" 
fi 
$  
$ ./test23.sh 
The square of 10 is 100 
$ 
$ cat test24.sh 
#!/bin/bash 
# using pattern matching 
# 
if [[ $USER == r* ]] 
then 
   echo "Hello $USER" 
else 
   echo "Sorry, I do not know you" 
fi 
$  
$ ./test24.sh 
Hello rich 
$ 

case 命令

case variable in 
pattern1 | pattern2) commands1;; 
pattern3) commands2;; 
*) default commands;; 
esac 
$ cat test26.sh 
#!/bin/bash 
# using the case command 
# 
case $USER in 
rich | barbara) 
   echo "Welcome, $USER" 
   echo "Please enjoy your visit";; 
testing) 
  echo "Special testing account";; 
jessica) 
   echo "Do not forget to log off when you're done";; 
*) 
   echo "Sorry, you are not allowed here";; 
esac 
$  
$ ./test26.sh 
Welcome, rich 
Please enjoy your visit 
$ 

 

标签:SHELL,testuser,检查,笔记,echo,学习,sh,file,fi
来源: https://www.cnblogs.com/aongao/p/11731957.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有