linux bash善用判断式

Posted 92#

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux bash善用判断式相关的知识,希望对你有一定的参考价值。

 

1.利用 test 指令的测试功能

$ test -e hello.sh && echo "ok" || echo "no"
ok

2.首先,判断一下,让使用者输入一个文件名,我们判断:

  1. 这个文件是否存在,若不存在则给予一个“Filename does not exist”的讯息,并中断程序;
  2. 若这个文件存在,则判断他是个文件或目录,结果输出“Filename is regular file”或 “Filename is directory”
  3. 判断一下,执行者的身份对这个文件或目录所拥有的权限,并输出权限数据!
#!/bin/bash
#Program
#       use input filename, program will check the flowing:
#       1)exist? 2)file/directory? 3)file permission
#Histroy:
#2017/07/28     lzyer   First release
echo "please input filename, i will check sth."
read -p "input filename: " filename
#1.
test -z ${filename} && echo "You MUST input filename" && exit 0
#2
test ! -e ${filename} && echo "the filename ‘${filename}‘ DO NOT exist " && exit 0
#3
test -f ${filename} && filetype="file"
test -d ${filename} && filetype="directory"
test -r ${filename} && perm="readable"
test -w ${filename} && perm="${perm}   writeable"
test -x ${filename} && perm="${perm}   executable"
echo "the filename:${filename} is ${filetype}"
echo "and permission for you are :${perm}"

3.利用判断符号 [ ]

[  "$HOME"  ==  "$MAIL"  ]
[□"$HOME"□==□"$MAIL"□]
 ↑       ↑  ↑       ↑

案例设置如下:

  1. 当执行一个程序的时候,这个程序会让使用者选择 Y 或 N ,
  2. 如果使用者输入 Y 或 y 时,就显示“ OK, continue ”
  3. 如果使用者输入 n 或 N 时,就显示“ Oh, interrupt !”
  4. 如果不是 Y/y/N/n 之内的其他字符,就显示“ I don‘t know what your choice is ”
#!/bin/bash
#Program:
#       This program shows "Hello World!" in your screen.
#History:
#       2017/07/29      lzyer  First release
read -p "Please input (Y/N)? " yn
[ "${yn}" == "Y" -o "${yn}" == "y" ] && echo "ok, continue " && exit 0
[ "${yn}" == "N" -o "${yn}" == "n" ] && echo "oh, interrupt "&& exit 0
echo "i don‘t know what you choice is " && exit 0

4.Shell script 的默认变量($0, $1...)

  • $# :代表后接的参数“个数”,以上表为例这里显示为“ 4 ”;
  • [email protected] :代表“ "$1" "$2" "$3" "$4" ”之意,每个变量是独立的(用双引号括起来);
  • $* :代表“ "$1<u>c</u>$2<u>c</u>$3<u>c</u>$4" ”,其中 <u>c</u> 为分隔字符,默认为空白键, 
  • 程序的文件名为何?
  • 共有几个参数?
  • 若参数的个数小于 2 则告知使用者参数数量太少
  • 全部的参数内容为何?
  • 第一个参数为何?
  • 第二个参数为何
#!/bin/bash
#Program
#       program show the script name,parameters
#Histroy
#       2017/07/29      lzyer   First release
echo "the script name is ==> ${0}"
echo "Total paramter number is ==> $#"
[ "$#" -lt 2 ] && echo "the number of parameters is less than 2.STOP here" && exit 0
echo "your whole parameter is ==> ‘[email protected]"
echo "The 1st parameter ${1}"
echo "The 2nd parameter ${2}"

5.shift:造成参数变量号码偏移

#!/bin/bash
#Program:
#       program show the effect of shift function.
#History:
#2017/07/29     lzyer   First release
echo "Total parameter number is $#"
echo "your whole parameter is [email protected]"
shift
echo "Total parameter number is $#"
echo "your whole parameter is [email protected]"
shift 2
echo "Total parameter number is $#"
echo "your whole parameter is [email protected]"

 



以上是关于linux bash善用判断式的主要内容,如果未能解决你的问题,请参考以下文章

-bash: /usr/bin/ls: /lib64/ld-linux-x86-64.so.2: bad ELF interpreter: No such file or directory(代码片段

bash

鸟哥的Linux私房菜——第十六章:学习Shell Scripts

sh Bash的代码片段

领导五式第二章读书笔记

Linux bash基础特性二