shell 学习 if /while / for 语法格式
2020-08-14 11:27
926 查看
语法特点总结
- 在大多数情况不需要 ;结尾
- shell脚本编程的空格非常重要,这和Linux的语法也有一定的关系。
- ? #!/bin/bash 的作用?
#!/bin/bash 是 Shell 脚本的第一行。
它的意思是命令通过 /bin/bash 来执行
if 语句示例:
if [条件1]; then 表达式1 elif [条件2];then 表达式2 else 表达式2 fi
if 判断目录、文件
#/bin/bash #auto testfile #by authors guoqi 2020 DIR=/tmp/20200714 if [ ! -d $DIR ];then mkdir -p $DIR echo"this $DIR creat success!" else echo -e "\033[32mthis $DIR is exist,please exit!\033[0m" fi FILES=/tmp/testfile.txt if [ ! -f $FILES ];then echo "OK" >> $FILES else echo -e "\033[32m-------------------\033[1m" cat $FILES fi
if 实现 switch
#/bin/bash #auto test switch use if esle #author by guoqi #scores=90 scores=$1 # -z 判断输入是否为空 if [ -z $scores ];then echo "usage:{$0 60|80}" exit fi # gt > if [[ $scores -gt 85 ]];then echo "very good" elif [[ $scores -gt 75 ]];then echo "good" elif [[ $scores -gt 60 ]];then echo "you pass" else echo "no pass" fi
while 循环语句
while [条件]: do 表达式 done
#!/bin/bash # 脚本生成一个 100 以内的随机数,提示用户猜数字,根据用户的输入,提示用户猜对了, # 猜小了或猜大了,直至用户猜对脚本结束。 # RANDOM 为系统自带的系统变量,值为 0‐32767的随机数 # 使用取余算法将随机数变为 1‐100 的随机数 num=$[RANDOM%100+1] echo "$num" # 使用 read 提示用户猜数字 # 使用 if 判断用户猜数字的大小关系:‐eq(等于),‐ne(不等于),‐gt(大于),‐ge(大于等于), # ‐lt(小于),‐le(小于等于) while : do read -p "计算机生成了一个 1‐100 的随机数,你猜: " cai if [ $cai -eq $num ] then echo "恭喜,猜对了" exit elif [ $cai -gt $num ] then echo "Oops,猜大了" else echo "Oops,猜小了" fi done
#!/bin/bash # 实时监控本机内存和硬盘剩余空间,剩余内存小于500M、根分区剩余空间小于1000M时,发送报警邮件给root管理员 # 提取根分区剩余空间 disk_size=$(df / | awk '/\//{print $4}') # 提取内存剩余空间 mem_size=$(free | awk '/Mem/{print $4}') while : do # 注意内存和磁盘提取的空间大小都是以 Kb 为单位 if [ $disk_size -le 512000 -a $mem_size -le 1024000 ] then mail ‐s "Warning" root <<EOF Insufficient resources,资源不足 EOF fi done
for 循环语句
for 变量 in 循环列表 do 表达式1 done
#!/bin/bash # 9*9 乘法表(编写 shell 脚本,打印 9*9 乘法表) for i in `seq 9` do for j in `seq $i` do echo -n "$j*$i=$[i*j] " done echo done
一键部署安装LNMP
#!/bin/bash # 一键部署 LNMP(源码安装版本) menu() { clear echo " ##############‐‐‐‐Menu‐‐‐‐##############" echo "# 1. Install Nginx" echo "# 2. Install MySQL" echo "# 3. Install PHP" echo "# 4. Exit Program" echo " ########################################" } choice() { read -p "Please choice a menu[1‐9]:" select } install_nginx() { id nginx &>/dev/null if [ $? -ne 0 ];then useradd -s /sbin/nologin nginx fi if [ -f nginx‐1.8.0.tar.gz ];then tar -xf nginx‐1.8.0.tar.gz cd nginx‐1.8.0 yum -y install gcc pcre‐devel openssl‐devel zlib‐devel make ./configure ‐‐prefix=/usr/local/nginx ‐‐with‐http_ssl_module make make install ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ cd .. else echo "没有 Nginx 源码包" fi } install_mysql() { yum -y install gcc gcc‐c++ cmake ncurses‐devel perl id mysql &>/dev/null if [ $? -ne 0 ];then useradd -s /sbin/nologin mysql fi if [ -f mysql‐5.6.25.tar.gz ];then tar -xf mysql‐5.6.25.tar.gz cd mysql‐5.6.25 cmake . make make install /usr/local/mysql/scripts/mysql_install_db ‐‐user=mysql ‐‐datadir=/usr/local/mysql/data/ ‐‐basedir=/usr/local/mysql/ chown -R root.mysql /usr/local/mysql chown -R mysql /usr/local/mysql/data /bin/cp -f /usr/local/mysql/support‐files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld /bin/cp -f /usr/local/mysql/support‐files/my‐default.cnf /etc/my.cnf echo "/usr/local/mysql/lib/" >> /etc/ld.so.conf ldconfig echo 'PATH=\$PATH:/usr/local/mysql/bin/' >> /etc/profile export PATH else echo "没有 mysql 源码包" exit fi } install_php() { #安装 php 时没有指定启动哪些模块功能,如果的用户可以根据实际情况自行添加额外功能如‐‐with‐gd 等 yum -y install gcc libxml2‐devel if [ -f mhash‐0.9.9.9.tar.gz ];then tar -xf mhash‐0.9.9.9.tar.gz cd mhash‐0.9.9.9 ./configure make make install cd .. if [ ! ‐f /usr/lib/libmhash.so ];then ln -s /usr/local/lib/libmhash.so /usr/lib/ fi ldconfig else echo "没有 mhash 源码包文件" exit fi if [ -f libmcrypt‐2.5.8.tar.gz ];then tar -xf libmcrypt‐2.5.8.tar.gz cd libmcrypt‐2.5.8 ./configure make make install cd .. if [ ! -f /usr/lib/libmcrypt.so ];then ln -s /usr/local/lib/libmcrypt.so /usr/lib/ fi ldconfig else echo "没有 libmcrypt 源码包文件" exit fi if [ -f php‐5.4.24.tar.gz ];then tar -xf php‐5.4.24.tar.gz cd php‐5.4.24 ./configure ‐‐prefix=/usr/local/php5 ‐‐with‐mysql=/usr/local/mysql ‐‐enable‐fpm ‐‐ enable‐mbstring ‐‐with‐mcrypt ‐‐with‐mhash ‐‐with‐config‐file‐path=/usr/local/php5/etc ‐‐with‐ mysqli=/usr/local/mysql/bin/mysql_config make && make install /bin/cp -f php.ini‐production /usr/local/php5/etc/php.ini /bin/cp -f /usr/local/php5/etc/php‐fpm.conf.default /usr/local/php5/etc/php‐fpm.conf cd .. else echo "没有 php 源码包文件" exit fi } while : do menu choice case $select in 1) install_nginx ;; 2) install_mysql ;; 3) install_php ;; 4) exit ;; *) echo Sorry! esac done
相关文章推荐
- Shell 语法 if 、 case 、for 、 while、 until 、select 、repeat、子函数
- Linux-shell学习4-条件判断 if case for while util
- android Monkeyrunner:Python语法知识学习(for,while,if用法实例)
- Shell 语法 if 、 case 、for 、 while、 until 、select 、repeat、子函数
- shell if,case,for,while语法
- linux shell学习 记录2 if elif case for while until 语法
- 学习笔记(10月25日)--python的if、while、for语法
- python 学习之流程控制if for while 及print 格式控制
- shell学习-9-if-case-for-while-until-break
- python学习(十)赋值、表达式、if、while、for
- Shell中关于if,case,for,while等的总结
- if条件和for循环语句、while、do..while、switch语法
- C/C++学习(一)题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
- linux shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select 学习笔记
- Shell中关于if,case,for,while等的总结
- [shell基础]——if/for/while/until/case 语句
- shell中if,for语法
- Shell脚本的基本语法(一) if判断、for循环、while循环和until循环
- Linux基础shell(下)以及if,for,while
- Python语法基础_控制流语句_if、while、for