In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
What are the conditional statements in Linux Shell programming? In order to solve this problem, the editor summarizes this article on conditional sentences today, hoping to help more friends who want to solve this problem to find a more simple and feasible way.
Conditional test file test integer test string and logic test IF statement if single branch statement if double branch statement if multi-branch statement if nested conditional test operation test command tests whether a specific expression is valid. When the condition is true, the return value of the test statement is 0 Otherwise, for other numeric formats 1: test conditional expression format 2: [conditional expression] / / there should be at least one space file to test [operator file or directory] commonly used test operator-d: test whether it is a directory-e: test whether a directory or file exists-f: test whether it is a file-r: test whether the current user has permission to read- W: test whether the current user has permission to write-x: test whether the current user has permission to execute [root@localhost ~] # [- d / media/cdrom] [root@localhost ~] # echo $? 0 / return 0 indicates that the condition holds [root@localhost ~] # [- d / media/cdrom/Server] [root@localhost ~] # echo $ ? 1 / / return 1 means that the condition is not valid [root@localhost ~] # [- d / media/cdrom] & & echo "YES" / & & Logic and "and" means YES integer value comparison [integer 1 operator integer 2] commonly used test operator-eq: equal to-ne: not equal to-gt: greater than-lt: less than-le: less than or equal to-ge: greater than or equal to [root@localhost ~] # who | wc-L7 [root@localhost ~] # [$(who | wc-1)-gt 5] & echo "Too many." / / the number of users is No > 5Too many. [root@localhost ~] # [$(who | wc-1)-ge 10] & & echo "> = 10." / / whether the number of users > = 10 [root@localhost ~] # FreeCC=$ (free-m | grep "Mem:" | awk'{print $6} `) [root@localhost ~] # [$FreeCC-It 1024] & echo ${FreeCC} MB / / whether free memory
< 1024MB 275MB字符串比较 格式1 [ 字符串1 = 字符串2 ][ 字符串1 != 字符串2 ] 格式2 [ -z 字符串 ]常用的测试操作符=:字符串内容相同!=:字符串内容不同,!号表示相反的意思-z:字符串内容为空[root@localhost ~]# echo $LANG //测试当前语言环境zh_ CN.UTF-8[root@localhost ~]# [ $LANG != "en.US" ] && echo "Not en.US"Not en.US[root@localhost ~]# read -p"是否覆盖现有文件(yes/no) ?"ACK //测试读入的字符串是否为yes是否覆盖现有文件(yes/no) ? yes[root@localhost ~]# [ $ACK = "yes" ] && echo "覆盖"覆盖逻辑测试格式1:[ 表达式1 ] 操作符号 [ 表达式2 ]...格式2:命令1 操作符 命令2...常用的测试操作符-a或&&:逻辑与,"而且"的意思-o或||:逻辑或,"或者"的意思!:逻辑否[root@localhost ~]#[ -dletc ] && [-r /etc] && echo "You can open it"[root@localhost~]#[ -d/etc ] II [-d /home] && echo "ok"if语句的分支结构单分支结构if 条件测试操作 then 命令序列 fi 单分支if语句判断挂载点目录,若不存在则自动创建[root@localhost ~]# vim test01.sh#!/bin/bashdir="/mnt/aaa/"if [ !-d $dir ] //当目录不存在时执行thenmkdir -p $dirif双分支结构if 条件测试操作 then 命令序列1 else 命令序列2fi双分支if语句判断目标主机是否存活,显示检测结果root@localhost ~]# vim test02.sh#!/bin/bashping -c 3 -i 0.2 -W 3 $1 &>/ dev/nullif [$?-eq 0] then / / judges the test results of the ping command, and gives different prompts echo "Host $1 is up."else echo" Host $1 is down. "fi multi-branch structure if conditional test operation 1 then command sequence 1elif conditional test operation 2 then command sequence 2else command sequence 3fi multi-branch if statement judgment score range Divide into three grades: excellent, qualified and unqualified.
[root@localhost ~] # vim test03.ShroupBash read-p "Please enter your score (0-100):" aif [$a-ge 85] & & [$a-le 10] thenecho "$a, excellent!" / / judge where the score lies, and give different grading results elif [$a-ge 70] & & [$a-le 84] thenecho "$a score, qualified!" Elseecho "$a score, not qualified!" Fi, after reading the above, do you have any further understanding of conditional statements in Shell programming? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel. Thank you for reading.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.