# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/ksh
# bash --version
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
# echo $SHELL
/bin/bash
# man bash|col -b > bashman
-------------------------------------
1) | 管道
# ps aux |grep vi|grep -v grep
2) > >> << <
# echo hello > a.txt
[root@teacher test]# cat a.txt
hello
# echo dajiahao >> a.txt
[root@teacher test]# cat a.txt
hello
dajiahao
# echo y > b.txt
# rm a.txt < b.txt
# cat << END > /test/c.sh //END是界位符
> dajiahao
> haha
> END
# cat c.sh
dajiahao
haha
---------------
例子
# cat a.sh
#!/bin/bash
echo "hello"
cat << END > /test/b.sh
#!/bin/bash
cp /etc/passwd /tmp/hahaha
END
chmod 777 /test/b.sh
/test/b.sh
---------------
3) ; 分割命令
# cd /samba;touch 123
4) "" '' ``
`` 调用命令的返回结果
"" \ $ ``
'' 强引用
# ls -l `find /etc -name *.conf`
# a=b
You have new mail in /var/spool/mail/root
[root@teacher samba]# echo $a
b
[root@teacher samba]# echo "$a"
b
[root@teacher samba]# echo '$a'
$a
5) ? 一个字符
# ls a?
a1 a2 a3 a5 a6 a7 a8 a9
6) * 任意字符
# ls a*
7) (cmds) [] {cmds}
[root@teacher test]# ( cd /samba; touch aaa )
# ls [a-z][0-9]*
a1 a11 a13 a15 a3 a6 a8 p1 p1.awk p3 s2
a10 a12 a13.awk a2 a5 a7 a9 p11.awk p2 s1 s3
8) :
# vim d.sh
#!/bin/bash
i=0
while :
do
let i=i+1
echo hello$i
sleep 1
done
9) &
10) && ||
# make && make install //&&之前的命令执行成功,执行&&后面的命令
# ls abcdefg || touch abcdefg
ls: abcdefg: No such file or directory
# ls abcdefg
11) $
# a=1
# echo $a
1
-------------------------------------
算术运算
1) $[] == $(())
[root@teacher test]# echo $[1+2]
3
[root@teacher test]# echo $[3*8]
24
[root@teacher test]# echo $((5*6))
30
# a=3
[root@teacher test]# b=6
[root@teacher test]# echo $[$a*$b]
18
[root@teacher test]# a=3
[root@teacher test]# b=6
[root@teacher test]# echo $[$a*$b]
18
[root@teacher test]# c=$[$a*$b]
You have new mail in /var/spool/mail/root
[root@teacher test]# echo $c
18
2) bc
123456*345678
42676023168
4^2
16
8^9
134217728
2^64
18446744073709551616
quit
3)
# echo $a
3
[root@teacher test]# let a=a+5
[root@teacher test]# echo $a
8
----------------------------
变量
直接引用
# user=root
[root@teacher test]# echo $user
root
间接引用
# a=b
# b=c
# echo ${!a}
c
全局变量 局部变量
# set |grep "a=b"
a=b
[root@teacher test]# env |grep "a=b"
[root@teacher test]# export a //把局部变量变成环境变量
You have new mail in /var/spool/mail/root
[root@teacher test]# env |grep "a=b"
a=b
[root@teacher test]# bash
[root@teacher test]# echo $a
取消变量
[root@teacher test]# unset a
[root@teacher test]# unset b
[root@teacher test]# echo $a
------------------------
# echo $PATH
# echo $PWD
/test
# echo $HOME
/root
# cd $HOME
# echo $SHELL
/bin/bash
[root@teacher ~]# echo $USER
root
You have new mail in /var/spool/mail/root
[root@teacher ~]# echo $LOGNAME
root
# echo $LOGNAME
root
# echo $PS1
[root@teacher ~]#PS1='\s-\v\$'
bash-3.2#PS1='[\u@\h \W]\$'
[root@teacher ~]#
---------------------
# vim cs.sh
#!/bin/bash
echo $0
echo $1
echo $10
echo ${10}
echo $#
echo $*
echo $@
#./cs.sh 1 2 3 4 5 6 7 8 9 a b c d
./cs.sh
1
10
a
13
1 2 3 4 5 6 7 8 9 a b c d
1 2 3 4 5 6 7 8 9 a b c d
-----------
$? 上一条命令执行的结果
[test]#lkaskjfahwliht
bash: lkaskjfahwliht: command not found
[test]#echo $?
127
[test]#cp /etc/passwd /tmp/aaa
[test]#echo $?
0
You have new mail in /var/spool/mail/root
[test]#cp /helloworld/good /home/haha
cp: cannot stat `/helloworld/good': No such file or directory
[test]#echo $?
1
$# 参数的个数
#./cs.sh 1 2 3 a t c
./cs.sh
1
10
6
1 2 3 a t c
1 2 3 a t c
[test]#echo $_
c
[test]#vim &
[2] 3851
You have new mail in /var/spool/mail/root
[test]#echo $!
3851
#echo $$ 当前shell的进程号
11557
-------------------------
变量替换
var 就是一个变量,word 表示一个值
格式
${var:-word}
#echo ${var}
You have new mail in /var/spool/mail/root
[test]#echo ${var:-word}
word
[test]#echo ${var}
[test]#echo "user is ${username:-tom}"
user is tom
You have new mail in /var/spool/mail/root
[test]#username=mike
[test]#echo "user is ${username:-tom}"
user is mike
----------------------
${var:=word}
#echo ${var}
[test]#echo ${var:=word}
word
You have new mail in /var/spool/mail/root
[test]#echo ${var}
word
--------------------
${var:+word}
#echo "user is ${username:+tom}"
user is tom
You have new mail in /var/spool/mail/root
[test]#echo ${username}
mike
----------------------
${var:?word}
#echo ${username2:?"word"}
bash: username2: word
----------------
${var:offset}
[test]#username=uplooking
You have new mail in /var/spool/mail/root
[test]#echo ${username}
uplooking
[test]#echo ${username:6}
ing
[test]#echo ${username:5}
king
[test]#echo ${username:5:2}
ki
-------------------------------
变量扩展
${var%模式} 表示从尾部开始,进行最短匹配,然后删除匹配部分
[test]#var="/root/bin/local/bin/kill"
You have new mail in /var/spool/mail/root
[test]#echo $var
/root/bin/local/bin/kill
[test]#echo ${var%bin/*}
/root/bin/local/
${var%%模式} 表示从尾部开始,进行最长匹配,然后删除匹配部分
[test]#echo $var2
/root/bin/local/
[test]#echo ${var%%bin/*}
/root/
${var#模式} 表示从头部开始,进行最短匹配,然后删除匹配部分
[test]#echo ${var#*bin/}
local/bin/kill
${var#模式} 表示从头部开始,进行最长匹配,然后删除匹配部分
#echo ${var##*bin/}
kill
----------------
显示变量值
echo
-e 支持转义字符的特殊含义
-n 输出不换行
[test]# echo "hello\nhello"
hello\nhello
You have new mail in /var/spool/mail/root
[test]#echo -e "hello\nhello"
hello
hello
[test]#echo -n "hello"
hello[test]#
读取变量值
read
格式
read 变量名
read -p "提示信息..." 变量名
read -t 时间(单位是秒) 变量名
read -p "提示信息..." -t 时间(单位是秒) 变量名
#read username
root
[test]#echo $username
root
[test]#read -p "请输入用户名:" username
请输入用户名:root
[test]#echo $username
root
#read -p "请输入密码:" -s password
#read -p "请输入密码:" -s password
请输入密码:
[test]#echo $password
redhat
#read -s -t 5 -p "请输入密码:" password
请输入密码:You have new mail in /var/spool/mail/root
[test]#read username password
root redhat
[test]#echo $username
root
[test]#echo $password
redhat
------------------------
[test]#vim s01.sh
#!/bin/bash
#shell name:s01.sh
#purpose:test shell script
#date:2013-08-27
#author:lyx
echo "hello world"
[test]#chmod +x s01.sh
[test]#./s01.sh
hello world
[test]#sh s01.sh
hello world
You have new mail in /var/spool/mail/root
[test]#bash s01.sh
hello world
[test]#. s01.sh
hello world
[test]#source s01.sh
hello world
------------------
#vim s02.sh
#!/bin/bash
if [ 3 -gt 2 ]
then
echo "hello"
fi
[test]#sh s02.sh
hello
# vim s03.sh
#!/bin/bash
read -p "输入a:" a
read -p "输入b:" b
if [ $a == $b ]
then
echo "hello"
else
echo "getout here"
fi