shell脚本相关知识


如何执行shell脚本

执行脚本有以下三种方式:

  • 直接执行(shell.sh文件必须具备可读与可执行 (rx) 的权限):
    绝对路径:运行/home/chinahadoop/shell.sh 来执行指令
    相对路径:cd到/home/chinahadoop/ ,使用 ./shell.sh 执行
  • bash执行:
    bash shell.sh
    sh shell.sh
  • source 执行:
    source shell.sh

shell脚本举例

#!/bin/bash 
# Shows "Hello World!" in your screen. 
echo -e "Hello World! \a \n"  
exit 0 
# User inputs 2 integer numbers; program will cross these two  
echo -e "You SHOULD input 2 numbers, I will cross them! \n"  
read -p "first number: " firstnu  
read -p "second number: " secnu 
total=$(($firstnu*$secnu))  
echo -e "\nThe result of $firstnu x $secnu is ==> $total" 
  • 第一行#!/bin/bash,告诉系统如何执行脚本。当采用直接执行(不是用类似sh xxx.sh这样的方式)时,此行必须。
  • 除了第一行的之外#都是注释,其他为程序部分。
  • exit 0,以0作为该脚本的执行返回值。执行正常退出返回0。一 个脚本执行完成,之后的shell进程可以通过$?获取执行结果。
  • firstnu和secnu为两个输入变量。$(())为执行数值运算。

test命令

test -e demo.txt
判断某个文件类型 :
-e 该文件是否存在?
-f 该文件是否存在且为文件(file)?
-d 该文件名是否存在且为目录(directory)?
-b 该文件是否存在且为一个 block device 装置?
-c 该文件是否存在且为一个 character device 装置?
-S 该文件是否存在且为一个 Socket 文件?
-p 该文件是否存在且为一个 FIFO (pipe) 文件?
-L 该文件是否存在且为一个连接文件?
判断文件权限:
-r 检查该文件是否存在且具有可读的权限?
-x 检查该文件是否存在且具有可执行的权限?
-u 检查该文件名是否存在且具有SUID的属性?
-g 检查该文件名是否存在且具有SGID的属性?
-k 检查该文件名是否存在且具有Sticky bit的属性?
-s 检查该文件是否存在且为非空文件?
两个文件之间比较:
-nt 判断file1 是否比 file2 新
-ot 判断file1 是否比 file2 旧
-ef 判断两个文件是否为同一个文件
整数之间的判断:
-eq 两数值相等(equal)
-ne 两数值不等(not equal)
-gt n1大于n2(greater than)
-lt n1小于n2(less than)
-ge n1大于等于n2(greater than or equal)
-le n1小于等于n2(less than or equal)
判断字符串:
`test -z strig 判断字符串是否为空?若 string 为空字符串,则为 true
test -n string 判断字符串是否非空?若 string 为空字符串,则为 false
test str1 = str2 判断str1是否等于str2,若相等,则返回 true test str1 != str2 判断str1是否不等于str2,若相等,则返回 false

判断符号

“[]”(基本跟test相同)

#!/bin/bash 
# This program shows the user's choice  
read -p "Please input (Y/N): " yn 
[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit  0 

shell脚本默认参数

比如,ls -al 其中-al就是shell脚本参数,那么我们自己写的脚本 参数该如何使用呢 ?
• /path/to/scriptname opt1 opt2 opt3 opt4
$0 $1 $2 $3 $4
$0:代表脚本程序本身 $1,$2,…:代表后面第一个参数,第二个参数,等等 $# :代表后接的参数个数,以上边为例,这里显示为4; $@ :代表 “$1” “$2” “$3” “$4” ,每个变量是独立的(用双引号括起来); $* :代表 “$1 $2 $3 $4”

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

./shift.sh one two three four five six

#!/bin/bash 
echo "Total parameter number is ==> $#"   
echo "Your whole parameter is ==> '$@'"   
#shift一个变量  
shift  
echo "Total parameter number is ==> $#"   
echo "Your whole parameter is ==> '$@'"   
shift 3 #shift三个变量  
echo "Total parameter number is ==> $#"   
echo "Your whole parameter is ==> '$@'" 

条件判断语句

if …. then

if [ 条件判断 ]; then
条件成立执行,命令;
fi 将if反过来写,就成为fi 结束if语句

#!/bin/bash 
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then  
 echo "OK, continue" 
 exit 0  
fi

多条件判断

if [ 条件1 ]; then
条件1成立执行,命令;
elif [ 条件2 ]; then
条件2成立执行,命令;
else
条件都不成立指定;
fi

#!/bin/bash 
read -p "Please input (Y/N): " yn  
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then  
 echo "OK, continue"  
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then  
 echo "Oh, interrupt!" 
else 
 echo "I don't know what your choice is"  
fi

case ….. esac判断

case $变量名称 in
“第一个变量内容”)
程序段
;;
“第二个变量内容”)
程序段
;;
*)
exit 1
;;
esac

#!/bin/bash 
case $1 in   
"hello") 
    echo "Hello, how are you ?" 
    ;;   
"") 
    echo "You MUST input parameters, ex> {$0 someword}"  
    ;; 
*)  
    echo "Usage $0 {hello}"   
    ;;  
esac 

函数功能

function fname() {
命令
}

#!/bin/bash 
function printc(){ 
 echo -n "Your choice is "  } 
 echo "This program will print your selection !"  
 case $1 in  
"one") 
     printc; echo $1 | tr 'a-z' 'A-Z' 
     ;;   
 "two") 
     printc; echo $1 | tr 'a-z' 'A-Z' 
     ;;   
 "three") 
     printc; echo $1 | tr 'a-z' 'A-Z' 
     ;;   
 *) 
     echo "Usage $0 {one|two|three}" 
     ;;   
 esac 

循环语句

while循环语句

while [ condition ]
do
命令
done

#!/bin/bash 
sum=0 # 总数 
i=0 # 1, 2, 3....  
while [ "$i" != "100" ] 
do 
    i=$(($i+1)) # 每次 i 都会增加 1  
done  
echo "The result of '1+2+3+...+100' is ==> $sum" 

until循环语句

until [ condition ]
do
命令
done

#!/bin/bash 
until [ "$yn" == "yes" -o "$yn" == "YES" ]   
do 
 read -p "Please input yes/YES to stop this program: " yn   done 
echo "OK! you input the correct answer."

for循环语句

for循环语法:
for var in con1 con2 con3 …
do
命令
done
1. 第一次循环时, v a r c o n 1 ; 2. , var的内容为con2;
3. 第三次循环时, $var的内容为con3;
4. ….

#!/bin/bash 
for animal in dog cat elephant   
do 
 echo "There are ${animal}s.... "   
done 
#!/bin/bash 
# 1. 先看看这个目录是否存在啊?  
read -p "Please input a directory: " dir  
if [ "$dir" == "" -o ! -d "$dir" ]; then 
 echo "The $dir is NOT exist in your system." 
 exit 1   
fi 
# 2. 开始测试~ 
filelist=$(ls $dir) # 列出所有在该目录下的文件名   
for filename in $filelist 
do 
 perm=""   
 test -r "$dir/$filename" && perm="$perm   readable"     
 test -w "$dir/$filename" && perm="$perm   writable"     
 test -x "$dir/$filename" && perm="$perm executable"    
 echo "The file $dir/$filename's permission is $perm " 
done

for ((初始值;条件;执行步长))
do
命令
done

#!/bin/bash 
read -p "Please input a number, I will count for 1+2+.. .+your_input: " nu 
sum=0  
for (( i=1; i<=$nu; i=i+1 )) 
do 
 sum=$(($sum+$i))   
done 
echo "The result of '1+2+3+...+$nu' is ==> $sum" 

shell脚本检查

sh [-nvx] scripts.sh
选项与参数:
-n :不执行script,仅查询语法的问题;
-v :在执行script前,先将scripts的内容输出到屏幕上;
-x :将使用到的script内容显示到屏幕上,这是很有用的参数;

猜你喜欢

转载自blog.csdn.net/cherry593/article/details/81604006