系统相关
首页 > 系统相关> > SHELL学习笔记二

SHELL学习笔记二

作者:互联网

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 语句块中的语句得以执行。 

  数值比较

$ 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 
# 
$ 

字符串比较

#字符串不等条件也可以判断两个字符串是否有相同的值。 
$ 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程序员的问题:  

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

文件比较

  复合条件测试

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

 if-then 的高级特性

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

 双括号命令符号

$ 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