====================
bash
shell脚本:把平时在命令行执行的命令放在一个文本文件内,此文件即shell脚本
注意:绝大部分是非交互式命令执行脚本# bash 脚本文件# ./脚本文件# 路径/脚本文件交互式命令敲完回车之后没有直接出结果,并且需要在输入其他内容的命令[root@web tmp]# passwd wChanging password for user w.New password: BAD PASSWORD: The password is a palindromeRetype new password: passwd: all authentication tokens updated successfully.非交互式命令敲完回车之后直接出结果[root@web tmp]# echo 1 | passwd --stdin wChanging password for user w.passwd: all authentication tokens updated successfully.
变量
变量名称=值变量名称命名规定:只能由数字字母和下滑线组成,不能以数字开头变量的调用: $变量名称如果变量名称边界线不明,需要加{}来区分:# echo ${n}um变量类型不需要定义,变量默认值可以是0或者空字符串[root@web tmp]# a=8
[root@web tmp]# echo $abc[root@web tmp]# echo ${abc}A
A
[root@web tmp]# echo $[${abc}+8]
8随机数变量# echo $RANDOM取消屏幕回显#stty -echo恢复屏幕回显#stty echo取1-10随机数# echo [$RANDOM%10+1]test命令详解可以用[]代替[ 5 -gt 3 ]数值比较-gt > greater than-lt < less than-ge >= greater than or equal-le <= less than or equal-ne != not equal-eq = equal字符串比较== 或者 =!=[root@web tmp]# a="/etc/passwd"[root@web tmp]# if [ $a == "/etc/passwd" ];then echo hello; fihello[root@web tmp]# if [ $a = "/etc/passwd" ];then echo hello; fihello文件比较-e exsit[root@web tmp]# if [ -e $a ];then echo hello; fihello[root@web tmp]# a="/etc/pas"[root@web tmp]# if [ -e $a ];then echo hello; fi
if判断
单条件判断if 命令;then命令1命令2命令3...fiif 命令;then命令1命令2命令3...else命令fi
多条件判断
if 命令;then命令1命令2elif 命令;then命令1 elif 命令;then命令1else命令fi多条件判断case $i in 1)命令命令;;2)命令命令;;3)命令命令;;*)命令命令esac[root@web tmp]# a=8[root@web tmp]# case $a in> 2)> echo hello> ;;> "abc") echo haha> ;;> 8) echo 就是他;;> *)> echo 你写错了> esac就是他布尔值0 1
==================
标准输出 &
标准正确 1执行命令所得正确结果
标准错误 2执行命令所得非正确结果
标准输入 0
[root@web tmp]# echo hello
hello
[root@web tmp]# echo hello 1>/dev/null
[root@web tmp]# ecfd hello 1>/dev/null
bash: ecfd: command not found...
[root@web tmp]# ecfd hello 2>/dev/null
[root@web tmp]# ecfd hello &>/dev/null
[root@web tmp]# echo hello &>/dev/null[root@web tmp]# if ls &>/dev/null;then echo hello; fi
hello
[root@web tmp]# if pwd &>/dev/null;then echo hello; fi
hello
获取上一条命令的返回值使用$?例子: [root@web tmp]# grep hello /etc/passwd[root@web tmp]# echo $?1[root@web tmp]# grep root /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin[root@web tmp]# echo $?0
======================