赞
踩
#!/bin/bash
# 上面用于指定该脚本的执行程序
echo "hello shell" # 输出,相当于println
num=10 # 定义变量
echo $num # $num 获取num的值
unset num # 清除变量num
echo $num
# 这是注释
read num
echo $num
# read用于从控制台读取数据,-p用于添加提示语
read -p "请输入数字" num1
echo "输入的数字是:$num1"
三种执行方式
三种执行方式的不同点
./xxx.sh :先按照 文件中#!指定的解析器解析,如果#!指定指定的解析器不存在 才会使用系统默认的解析器
bash xxx.sh:指明先用bash解析器解析,如果bash不存在 才会使用默认解析器
. xxx.sh 直接使用默认解析器解析(不会执行第一行的#!指定的解析器)但是第一行还是要写的
三种执行情况
打开终端就会有以后个解释器,我们称为当前解释器,我们指定解析器的时候(使用 ./xxx.sh 或 bash xxx.sh)时会创建一个子shell解析 脚本
#!/bin/bash # 获取执行该脚本文件时,传递的参数 # ./脚本文件.sh 参数1 参数2 ... echo "参数的个数:$#" echo "参数的内容:$*" echo "第一个参数:$1" echo "第二个参数:$2" # 定义只读变量,该变量不可被修改。 readonly num=10 # 该语句会报错误 num=100 # $?用来获取上一条指令的执行结果,0表示成功,其余数值表示失败。 echo "num=100命令执行的结果为:$?" echo "当前进程名:$0" echo "当前进程号:$$"
#!/bin/bash
# 特殊标记
# ""中的内容会被解析,''中的内容不会被解析,``中的内容会作为系统命令执行
echo '单引号'
echo "日期:`date`"
# \为转义字符,必须加上-e才有效
echo "#\n#"
echo -e "#\n#"
echo -e "\n"
ls
echo -e "/n"
#!/bin/bash # ()中的脚本会在新的子shell中执行,不会影响当前shell num1=10 ( num1=100 echo "()中的num1=$num1" ) echo "当前shell中的num1=$num1" # {}中的脚本会在当前shell执行,会对当前shell造成影响 num2=10 { num2=100 echo "{}中的num2=$num2" } echo "当前shell中的num2=$num2" # ${num:-val}当num不存在时,整个表达式的值为val,当num存在时,整个表达式的值为num echo ${num3:-123} num3=111 echo ${num3:-123} # ${num:=val}当num不存在时,表达式的值为val,并且将val赋值给num echo ${num4:=444} echo $num4
#!/bin/bash # 字符串操作,不影响原字符串 str='hehe:haha:lala:xixi' # 获取字符串的长度 echo $str echo "str字符串的长度为${#str}" # 截取字符串 echo "str从下标为3的地方开始截取:${str:3}" # 截取指定长度字符串 echo "str从下标为3的地方开始截取长度为6个字节的字符串:${str:3:6}" # 替换字符串 ${str/oldstr/newstr} 用newstr替换str中第一个oldstr echo "将str中第一个:替换为#,${str/:/#}" echo $str # 替换所有old ${str//oldstr/newstr} 用newstr替换str中所有的oldstr echo "将str中所有的:替换为#,${str//:/#}" echo $str
#!/bin/bash # test命令用于判断字符串、文件状态、数字 # 格式:test 条件 或者[ 条件 ],注意中括号左右有空格。 # 一、判断文件 # 判断文件时条件列表: # -e是否存在,-d是否是目录,-f是否是文件,-r是否可读,-w是否可写,-x是否可执行,-L符号连接,-c是否字符设备,-b是否块设备,-s文件非空 read -p "请输入文件名:" fileName test -e $fileName [ -e $fileName ] echo "$fileName文件是否存在:$?" read -p "请输入文件或文件夹名:" fileName1 test -d $fileName1 echo "$fileName1是否是文件夹?$?" [ -f $fileName1 ] echo "$fileName1是否是文件$?"
#!/bin/bash
# 二、判断字符串
# 判断字符串时条件列表:
# str1 = str2判断两个字符串是否相等,!=判断两个字符串是否不等,-z判断字符串是否是空字符串,-n判断字符串是否不是空字符串
read -p "请输入第一个字符串" str1
read -p "请输入第二个字符串" str2
test "$str1" = "$str2"
echo "俩个字符串是否相等?$?"
[ "$str1" != "$str2" ]
echo "两个字符串是否不等?$?"
test -z $str2
echo "第二个字符串是否是空字符串?$?"
[ -n $str1 ]
echo "第一个字符串是否不是空字符串?$?"
#!/bin/bash
# 三、判断数值
# 判断数值时条件列表:
# num1 -eq num2数值相同,-ne数值不相等,-gt数1大于数2,-ge数1大于等于数2,-le数1小于等于数2,-lt数1小于数2
read -p "请输入第一个数" num1
read -p "请输入第二个数" num2
test "$num1" -eq "$num2"
echo "两个数值是否相等?$?"
[ "$num1" -ne "$num2" ]
echo "两个数值是否不相等?$?"
test "$num1" -ge "$num2"
echo "数1是否大于等于数2?$?"
[ "$num1" -lt "$num2" ]
echo "数1是否小于数2?$?"
#!/bin/bash
# 四、多条件判断
# $$左边为true时右边才会执行,||左边为false时右边才会执行
test 1 -eq 1 && echo "左边为true"
test 1 -ne 1 && echo "左边为false"
test 1 -eq 1 || echo "||左边为true"
test 1 -ne 1 || echo "||左边为false"
test 1 -ne 1 || test 2 -eq 2 && echo "左边整体结果为true"
#!/bin/bash
# 五、多重条件判断
# -a两边都要满足才为true,-o两边满足任意一边就为true,!取反
read -p "请输入文件名" fileName2
test -r $fileName2 -a -x $fileName2
echo "$fileName2文件可读并且可执行?$?"
[ -r $fileName2 -o -x $fileName2 ]
echo "$fileName2文件可读或可执行?$?"
test ! -x $fileName2
echo "$fileName2文件不可执行?$?"
#!/bin/bash # 一、控制语句 # 1、if....else # 格式:if 条件;then 语句1 elif 条件2; then 语句2 else 语句3 fi read -p "请输入y继续执行:" str if [ "$str" = 'y' ]; then echo "继续执行" else echo "停止执行" fi read -p "请输入文件夹名称:" fileName if test -e $fileName -a -d $fileName; then echo "$fileName文件夹存在并且是文件夹,进入该文件夹" cd $fileName echo "在$fileName文件夹下创建test.txt文件" touch test.txt else echo "$fileName文件夹不存在或者不是文件夹,即将创建该文件夹并进入" mkdir $fileName if [ $? -eq 0 ]; then cd $fileName echo "在fileName文件夹下创建test1.txt文件" touch test1.txt else echo "$fileName文件夹创建失败" fi fi # 使用tree命令需要安装tree,yum install tree # -C 表示颜色区分文件类型 -p表示显示文件权限 tree -C -p
#!/bin/bash
# 2、case 多支路判断
# 格式:case $变量 in "第一个比对值" ) 程序段1 ;; "第二个比对值") 程序段2 ;; .... *) 其他程序段 exit1 esac
read -p "请输入yes/no:" str
case $str in
yes | y* | Y* )
echo "输入了yes"
;;# 两个分号等同于break
no | n* | N* )
echo "输入了no"
;;
* )# 等同于default
echo "输入了其他"
exit 1 # 直接退出shell,下面的语句不再执行
esac
#!/bin/bash # 二、循环语句 # 1、for循环 # 1.1、格式1:for (( 初始值;限制值;步进值)) do 程序段 done declare -i sum=0 # declare 是bash内建的指令,用来声明shell变量 -i表示强制把变量作为int类型运算,declare可以用typeset替换 declare -i i=0 for (( i=0; i<=10; i++ )) do sum=$sum+$i done echo "第一种格式计算值:$sum" # 1.2、格式2:for var in con1 con2 con3 ... do 程序段 done typeset -i sum1=0 typeset -i i1=0 for i1 in 1 2 3 4 5 6 7 8 9 10 do sum1=$sum1+$i1 done echo "第二种格式计算值:$sum1" # for循环遍历文件夹 for fileName in `ls` do if [ -d $fileName ];then echo "$fileName是文件夹" elif [ -f $fileName ];then echo "$fileName是文件" else echo "$fileName属性未知" fi done
#!/bin/bash # 2、while循环 # 格式:while 条件 do 程序块 done 当条件成立时循环,条件不成立时退出循环 declare -i num=1 while [ $num -le 10 ] do # $(())用于运算括号里的值 if [ $(($num%2)) -eq 0 ];then num=$num+1 continue # 跳出本次循环 elif [ $num -eq 5 ];then break # 跳出整个循环 fi echo $num num=$num+1 done
#!/bin/bash
# 3、until循环
# 格式:until 条件 do 程序块 done 直到条件成立才会结束循环
declare -i num1=2
until [ $num1 -gt 10 ]
do
echo $num1
num1=$num1+2
done
#!/bin/bash # 函数定义及使用 # 一、定义 # 格式:function 函数名(){程序块} 或者 函数名(){程序块} # 参数使用,不需要在括号中定义参数,直接使用$1、$2、$3、$4、$5、$6、...来代表传递的第几个参数 # 返回值 return 返回值,在调用函数后使用$?获取返回值 # 注意:函数定义必须在函数调用的前面,shell解析到函数定义后才可以调用函数 function max(){ if [ $1 -ge $2 ];then return $1 else return $2 fi } # 二、使用 read -p "请输入两个整数(用空格隔开):" num1 num2 max $num1 $num2 echo "最大的是:$?"
#!/bin/bash
# 三、定义函数在另一个脚本文件中
# 将另一个脚本文件导入后就可以调用函数了。
source test6.sh
min $num1 $num2
echo "最小值是:$?"
test6.sh文件
#!/bin/bash
function min(){
if [ $1 -gt $2 ];then
return $2
else
return $1
fi
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。