SHELL编程

创建脚本

vi helloworld.sh

#脚本以#!/bin/bash开头(指定解析器)

#!/bin/bash
echo "hello world"
touch 1.txt 2.txt 3.txt
ls -l > 1.txt
**

执行脚本

第一种方式 分配执行权
chmod u+x helloworld.sh
./helloworld.sh
/mysh/helloworld.sh

第二种方式 可以没有执行权 
bash  helloworld.sh
sh helloworld.sh

系统变量

使用set可以查看所有系统变量
BASH=/bin/bash
HOME=/root
HOSTNAME=linux01
JAVA_HOME=/opt/apps/jdk1.8.0_191
PATH=/usr/local/sbin:/usr/local

自定义变量

变量定义 
	变量名=变量值  #注意不能有空格
删除变量 
	unset 变量名 
声明常量 readonly变量,不能使用unset变量

name=zss
name=lss  改值
age=23
gender=M
readonly  USERNAME=Ly 不可变的变量  常量 

取值 
$name
${name}
"$name" 
'$name' ---> $name的字符串
注意字符串的拼接

echo $name    lss
echo ${name}  lss
echo "$name"aaa lssaaa  ""号中取变量的值可以正常取出  
echos '$name'aaa  $nameaaa ''号中取不出来变量的值 就是$name字符串 单引号会将所有特殊字符脱意



删除只读变量 (了解)
yum -y install gdb 

cat << EOF|gdb
attach $$
call unbind_variable("username")
detach
EOF
unset username

export和source**

export  username=lisi 将变量的范围作用在所有的子bash中
source  将子bash定义的变量 作用在当前bash   

export 修饰的变量的作用域是 当前和所有的子进程
source 将子进程中的变量    拉取到当前

特殊变量

$1  $2  $n  ${10}  接收单个参数
$*   接收所有参数 
$@   接收所有参数
$#   参数的个数 
$?    接收上个命令执行的结果  0 执行成功   非0的是执行失败 

read交互

#!/bin/bash
read -t 10 -p '请输入您的用户名:' username   #username用来接收用户输入的用户名  -t 10代表10秒不输退出
read -t 10 -p '请输入您的密码:' password
echo "$username"
echo "$password"

算数运算

expr 1+1   # 错误  1 + 1 之间必须有空格
expr 1 + 1  
expr 1 + 1 \* 2 由于*有特殊含义 需要转义 
expr `expr 1 + 2` \* 3

$((1+1))  
$(((1+2)*3))
$[1+1]
$[(1+2)*3]


${a}  取变量a的值
$() 取一个命令的结果  
$(( )) 取算术运算表达式的运算结果  $[]相同

条件判断

&&:用来执行条件成立后执行的命令
||:用来执行条件不成立后的执行命令

ping windows && echo yes || echo no    windows能ping通 输出yes 不能ping通输出no
ls && echo yes || echo no  ls执行成功 输出yes 执行不成功输出no

整数测试

注意 符号 两边要有空格
test 1 = 1 && echo yes || echo no 			 1 == 1
test 1 != 1 && echo yes || echo no 		 	 1 != 1
test 1 -eq  1 && echo yes || echo no 		 1 == 1
test 1 -ne  1 && echo yes || echo no 		 1 != 1
test 1 -gt  2 && echo yes || echo no 		 1 >  2
test 1 -lt  2 && echo yes || echo no 		 1 <  2
test 1 -ge  2 && echo yes || echo no 		 1 >= 2
test 1 -le  2 && echo yes || echo no 		 1 <= 2


a=1
b=2
test $a = $b  && echo yes || echo no 
test $a != $b  && echo yes || echo no 

test $a   && echo yes || echo no  a存在 输出 yes  不存在输出 no

test $a -a $a = 1   && echo yes || echo no       yes
test $a -a $a = 2   && echo yes || echo no       no
test $a -o $a = 2   && echo yes || echo no 		yes

test命令通常做判断, test 命令和 [ ] 等同
#前后必须有空格
[1 = 1] && echo yes || echo no 	 错误
[ 1 = 1 ] && echo yes || echo no 	正确

字符串测试

test "abc" == "bcd" && echo yes || echo no    判断字符串相等 注意 == 两边要有空格
test "abc" != "bcd" && echo yes || echo no    判断字符串不相同 

判断字符串不是null 
 test $abc  && echo yes || echo no    abc 存在 输出 yes 不存在输出 no
 判断字符串是null
 test -z $abc  && echo yes || echo no   abc不存在 输出yes 存在输出 no
 [ $abc ] && echo yes || echo no  不是空 yes  是空 no
 
 if(name !=null ){
   
 }

文件测试

-d 判断是否是文件夹
-f 判断是否是文件 
-L 判断是否是超链接 快捷方式
-e 判断是否存在
-r 判断是否有读权限
-w 判断是否有写权限 
-x 判断是否是执行权

test -d 1.txt && echo yes || echo no  
[ -d 1.txt ]  && echo yes || echo no  
[ -f 1.txt ]  && echo yes || echo no  
[ -e 1.txt ]  && echo yes || echo no  
[ -L /bin ] && echo yes || echo no  
[ -r 1.txt -a -w 1.txt ] && echo yes || echo no 
[ -x 1.txt ] && echo yes || echo no

if语句判断

单条件判断
if [ 条件 ]
 then 
 	执行
fi

互斥条件判断
if [ 条件 ]
then 
	程序
else 
	程序
fi

#!/bin/bash
read -p '请输入您的年龄:' AGE
if [ $AGE -ge 18 ]
  then
        echo "你成年了可以看片了"
  else
        echo "你还未成年 滚蛋"
fi

#判断传入的参数 $1是否存在 存在打印值 不存在 则输出不存在
#!/bin/bash
name=$1
if [ $name ]
    then
        echo "$name"
    else
        echo "name不存在"
fi

多条件判断
if [ 条件1 ]
  then 
  	  执行 
 elif [ 条件2 ]
  then 
  	 执行
 ...
 else 
 	执行	
fi

#!/bin/bash
if [ $1 -ge 90 -a $1 -le 100 ]
        then
             echo  "优秀"
elif [ $1 -ge 80 -a $1 -lt 90 ]
        then
              echo  "良好"
elif [ $1 -ge 60 -a $1 -lt 80 ]
        then
             echo  "及格"
elif [ $1 -ge 0 -a $1 -lt 60 ]
        then
              echo "潜力非常大"
else
        echo "分数有误"
fi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值