shell 脚本文件

https://blog.csdn.net/qq_18297675/article/details/52693464

编写shell脚本的时候,最前面要加上一行:#!/bin/bash,因为linux里面不仅仅只有bash一个解析器,还有其它的,它们之间的语法会有一些不同,所以最好加上这一句话,告诉系统要用这个解析器。

一.shell变量

shell变量和一些编程语言不同,一般shell的变量赋值的时候不用带“$”,而使用或者输出的时候要带“$”。加减乘除的时候要加两层小括号。括号外面要有一个“$”,括号里面的变量可以不用“$”。需要注意的是,变量赋值,变量使用的时候不能有空格,否则会被解析成命令,报错无此命令。

例子:

#!/bin/bash

a=10
b=20
c="this is a test"
d=$((a+b))
e=$((a-b))
f=$((a*b))
g=$((a/b))
h=$((a%b))
i=$((a**3))

echo $c
echo "a = "$a          #输出a的值
echo "b = "$b          #输出b的值
echo "a+b = "${d}      #输出a+b的值
echo "a-b = "${e}      #输出a-b的值
echo "a*b = "${f}      #输出a*b的值
echo "a/b = "${g}      #输出a/b的值
echo "a%b = "${h}      #输出a%b的值
echo "a^3 = "${i}     #输出a的3次方的值

echo "a+b = "$((a+b))  #输出a+b的值
echo "a-b = "$((a-b))  #输出a-b的值
echo "a*b = "$((a*b))  #输出a*b的值
echo "a/b = "$((a/b))  #输出a/b的值
echo "a%b = "$((a%b))  #输出a%b的值
echo "a^3 = "$((a**3)) #输出a的3次方的值

echo $((a+b*a-b/a+a%b+a**2)) #表达式可以很长
 

二.shell变量表达式

例子:

#!/bin/bash

str="a b c d e f g h i j"

echo "the source string is "${str}                         #源字符串
echo "the string length is "${#str}                        #字符串长度
echo "the 6th to last string is "${str:5}                  #截取从第五个后面开始到最后的字符
echo "the 6th to 8th string is "${str:5:2}                 #截取从第五个后面开始的2个字符
echo "after delete shortest string of start is "${str#a*f} #从开头删除a到f的字符
echo "after delete widest string of start is "${str##a*}   #从开头删除a以后的字符
echo "after delete shortest string of end is "${str%f*j}   #从结尾删除f到j的字符
echo "after delete widest string of end is "${str%%*j}     #从结尾删除j前面的所有字符包括j
 

三.shell测试判断test或[]

需要注意的是使用[]的时候必须要每个变量之间都要有空格,和左右中括号也要有空格,否则报错。


echo "Please input a filename: "
read filename
echo "by test\n"
test -f $filename && echo "the file is ordinary file" || echo "the file is not ordinary file"
test -d $filename && echo "the file is document folder" || echo "the file is not document folder"
test -r $filename && echo "the file can read" || echo "the file can not read"
test -w $filename && echo "the file can write" || echo "the file can not write"
test -x $filename && echo "the file can executable" || echo "the file can not executable"

echo "by []\n"
[ -f $filename ] && echo "the file is ordinary file" || echo "the file is not ordinary file"
[ -d $filename ] && echo "the file is document folder" || echo "the file is not document folder"
[ -r $filename ] && echo "the file can read" || echo "the file can not read"
[ -w $filename ] && echo "the file can write" || echo "the file can not write"
[ -x $filename ] && echo "the file can executable" || echo "the file can not executable"

echo "Please input two numbers:"
read num1
read num2

echo "num1 = "${num1}
echo "num2 = "${num2}
echo "by test\n"
test $num1 -eq $num2 && echo "num1 == num2" || echo "num1 != num2"
test $num1 -ne $num2 && echo "num1 != num2" || echo "num1 == num2"
test $num1 -gt $num2 && echo "num1 > num2" || echo "num1 <= num2"
test $num1 -lt $num2 && echo "num1 < num2" || echo "num1 >= num2"
test $num1 -ge $num2 && echo "num1 >= num2" || echo "num1 < num2"
test $num1 -le $num2 && echo "num1 <= num2" || echo "num1 > num2"

echo "by []\n"
[ $num1 -eq $num2 ] && echo "num1 == num2" || echo "num1 != num2"
[ $num1 -ne $num2 ] && echo "num1 != num2" || echo "num1 == num2"
[ $num1 -gt $num2 ] && echo "num1 > num2" || echo "num1 <= num2"
[ $num1 -lt $num2 ] && echo "num1 < num2" || echo "num1 >= num2"
[ $num1 -ge $num2 ] && echo "num1 >= num2" || echo "num1 < num2"
[ $num1 -le $num2 ] && echo "num1 <= num2" || echo "num1 > num2"
 

四.shell条件分支结构语句

1.单分支判断语句

格式:if 条件 ; then 结果 fi ,最后面一定要有fi,在shell脚本里面,控制分支结构结束都要和开头的单词相反,例如,if <–> fi,case <–> esac。

#!/bin/bash

echo "Please input a filename"
read filename
if [ -f $filename ];then
echo "this file is a ordinary file."
fi
 

2.双分支判断语句

echo "Please input a filename"
read filename
if [ -f $filename ];then
echo "this file is a ordinary file."
else
echo "this file is not a ordinary file."
fi
 

3.多分支判断语句

多分支判断有两种,和C语言的一样 if else if,case。只是形式上有一些不同。

#!/bin/bash

echo "Please input your math grades"
read grades

if [ $grades -gt 100 ] || [ $grades -lt 0 ];then
echo "Please input the number range in 0 - 100"
fi

if [ $grades -ge 90 ] && [ $grades -le 100 ];then
echo "Your grade is excellent."
elif [ $grades -ge 80 ] && [ $grades -le 89 ];then
echo "Your grade is good."
elif [ $grades -ge 70 ] && [ $grades -le 79 ];then
echo "Your grade is middle."
elif [ $grades -ge 60 ] && [ $grades -le 69 ];then
echo "Your grade is passing."
else
echo "Your grade is badly."
fi
 

#!/bin/bash

echo "Please input a command"
read cmd
case $cmd in
cpu)    echo "The cpu information is"
        cat  /proc/cpuinfo;;
mem)    echo "The mem information is"
        cat /proc/meminfo;;
device) echo "The device information is"
        cat /proc/scsi/device_info;;
CD-ROM) echo "The CD-ROM information is"
        cat /proc/sys/dev/cdrom/info;;
*)      echo "Your input command is invalid"
esac
 

五.shell循环语句

1.while语句

while语句是只要条件为真就执行下面语句。 
格式: 
while 条件 
do 
语句 
done

需要注意的是,这里的条件除了 while true 可以这样写,其它的条件都要用 test或者 []来判断

#!/bin/bash

i=$1
while [ $i -gt 0 ]
do
echo $i
((i--))
done
 


2.until语句

until语句是只要条件为假就执行下列语句 
格式: 
until 条件 
do 
语句 
done

#!/bin/bash

i=$1
until [ $i -le 0 ]
do
echo $i
((i--))
done
 

3.for语句

格式: 
for 变量 in 列表 
do 
语句 
done

#!/bin/bash


for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符
do
   echo $i
done
 

六.shell函数

格式: 
[function] funcName() 

语句 
[return 返回值] 

返回值是可选的,如果没有显示return 则默认返回最后一条语句执行的结果。

Shell 函数返回值只能是整数,一般用来表示函数执行成功与否,0表示成功,其他值表示失败。如果 return 其他数据,比如一个字符串,往往会得到错误提示:“numeric argument required”。

如果一定要让函数返回字符串,那么可以先定义一个变量,用来接收函数的计算结果,脚本在需要的时候访问这个变量来获得函数返回值。

函数参数从$1到$n,$0 是文件名。

例子:

#!/bin/bash

#打印数字
printNum()
{
   echo $1
}

for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符
do
printNum $i
done
 

返回字符串,报错

#!/bin/bash

#打印字符串
printNum()
{
   return "Hello"
}

for i in `seq 2 8` #seq是一个命令,顺序生成一串数字或者字符
do
printNum
done
 

 

PlistBuddy 命令

查看帮助

/usr/libexec/PlistBuddy --help

 

打印文件

/usr/libexec/PlistBuddy -c "print" info.plist

 

添加普通的字段

/usr/libexec/PlistBuddy -c 'Add :Version string 1.0' info.plist

 

  • 添加数组字段,分两步走,注意:key之间用 : 隔开,且不能有空格

# 先添加key值

/usr/libexec/PlistBuddy -c 'Add :Application array' info.plist

# 添加value值

yans67deMacBook-Pro:needfiles huangyg$ /usr/libexec/PlistBuddy -c 'Add :Application: string app1' info.plist

yans67deMacBook-Pro:needfiles huangyg$ /usr/libexec/PlistBuddy -c 'Add :Application: string app2' info.plist

yans67deMacBook-Pro:needfiles huangyg$ /usr/libexec/PlistBuddy -c 'Add :Application: string app3' info.plist

 

添加字典字段,分两步走

# 先添加key值

/usr/libexec/PlistBuddy -c 'Add :Person dict' info.plist

# 添加value值,

/usr/libexec/PlistBuddy -c 'Add :Age string secret' info.plist

/usr/libexec/PlistBuddy -c 'Add :Person:Name string yans67' info.plist

/usr/libexec/PlistBuddy -c 'Add :Person:sex string boy' info.plist

/usr/libexec/PlistBuddy -c 'Add :Person:weight string 65' info.plist

 

输出

 

/usr/libexec/PlistBuddy -c 'Print :Person' info.plist

 

  • 在array中我们还可以根据下标打印某个特定的值

/usr/libexec/PlistBuddy -c 'Print :Application:2' info.plist

 

删除字段相应的值

/usr/libexec/PlistBuddy -c 'Delete :Version' info.plist

 

  • 修改某个字段相应的值:

/usr/libexec/PlistBuddy -c 'Set :Application:1 string "thi is app1"' info.plist

  • 当有两个plist文件的时候,我们可以对其进行合并操作

# 将A.plist 合并到 B.plist中

/usr/libexec/PlistBuddy -c 'Merge A.plist'  B.plist

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值