shell:
cat /etc/shells #查看本机所有的Shell
echo $SHELL #查看当前的SHELL
/bin/csh #切换成csh
exit #退出子shell
文件权限:
ls -l 查看文件列表
ls -lh 装B转换成M
chmod [u,g,o,a] [+,-,=] [r,w,x,s,t] filename
chmod mode filename
mode
-r/4
-w/2
-x/1
eg:chomd +744 myfile
chown/chgrp
chown [-R] owner myfile #[-R]递归
chown owner.group myfile
chown owner.group myfile
chgrp [-R] .group myfile
umask
/etc/profile($HOME/.profile
$HOME/.bash_profile)
umask
符号链接
ls [-s] source_path target_path
shell脚本基本元素
eg:
#!/bin/bash
#名称
...
chomd u+x helloword.sh
./helloword.sh
shell的特性
1.别名
alias($HOME/.bashrc)
eg:
alias ll="ls -alh"
2.命令替换
myfile(的内容):
parm
findfile
eg:ls `cat myfile` -al
3.后台处理
nohup command &
jobs -l
4.变量
printer="helloworld!"
5.管道
ls|sort
6.重定向
< >
eg:sort <abc > xxx.txt
7.模式匹配
ls *.txt
8.特殊字符
"/'/`/\/&/;/()/{}/|/<>/*?[]!/$/#/ /
--------------------------------------------------------------------------------------
标准变量:
EXINIT
HOME #用户主目录
IFS #默认以什么分割
LOGNAME #以哪个用户登录的 set | grep "LOG"
MAIL #当前用户的邮箱放到哪个位置
MAILCHECK
MAINPATH
PATH
PS1 #默认窗口显示
PS2
SHELL
TERMINAL
TERM
TZ
EDITOR
PWD
MANPATH
特殊变量:
$# $* $$ $! $@ $- $?
eg:
#!/bin/bash
#parm
echo "this is script name:$0"
echo "this is script the 1 param:$1"
echo "this is script the 2 param:$2"
echo "this is script the 3 param:$3"
echo "this is script the 4 param:$4"
echo "this is script the 5 param:$5"
echo "this is script the 6 param:$6"
echo "this is script the 7 param:$7"
echo "this is script the 8 param:$8"
echo "this is script the 9 param:$9"
echo " all param nums:$#"
echo "all param:$*"
echo "thread id is:$$"
echo "display current task the last status:$?"
影响变量的命令:
1.declare 设置和显示变量
-f 只显示函数名
-r 创建只读变量
-x 创建转出变量
-i 创建整数变量
- +代替-,可以颠倒选项的含义
2.export 用户创建传给子shell的变量
--表明选项结束,所有后续参数都是实参
-f表明在“名-值"对中的名字是函数名。
-n把全局变量转换成局部变量。命令的变量不再传给shell
-p 显示全局变量列表
3.readonly 用于显示或设置只读变量
--表明选项结束
-f 创建只读变量
4.set 设置或重高各种Shell
5.shift [n] 用户移动位置变量,调整位置变量,使$3的值赋予$2,$2的值赋予$1
6.typeset 用户显示或设置变量,是declare的同义词
7.unset 用于取消变量的定义
--表明选项结束
-删除只读变量,但不能取消从Shell环境中删除指定的变量和函数。
如PATH,PS1,PS2,PPID,UID,EUID等的设置
===》引号
1.echo ert * echo "ert *"
2.双引号,使用双引号可引用的除字条$、`、\外的任意字符串或者字符。
echo -e "ert,$SHELL'\n* china `echo sigangjun`"
3.单引号。与双引号类似,不同的是shell会忽略任何引用值,如果屏蔽了其特殊含义,会将引号里的所有字符,包括引号都作为一个字符串。
echo -e 'ert,$SHELL'\n* china `echo sigangjun`'
4.反引号。设置系统命令的输出到变量。shell将反引号中的内容作为一个系统命令,并执行其内容。
echo "* china `echo sigangjun`"
5.反斜杠。屏蔽特殊含义 & * + ^ $ ` " | ?
echo *
echo \*
===》运算符
1.位运算符。
1.1 ~ << >> & | ^
~反运算符 eg:~opt1
<< 左移运算符
>> 右移运算符
& 与比较运算符
^ 异或运算符
| 或运算符
1.2 $[]
eg: $[2+8]
1.3 逻辑运算符
&&与运算符
||或运算符
> == < ! =
1.4 赋值运算符
= += -= *= /= %= &= ^= |= <<= >>=
let $count = $count + $change
let $count + = $change
1.5 表达式替换
$[] 和 $( () )
$[base#n] n表示基数从2到36的任何基数
echo $[10#8+1] ====> 9
1.6 优先级
===》Shell 输入与输出
1.echo
-e 解析转义字符
-n 回车不换行,默认是回车换行
-转义符 (\c,\f,\t,\n)
2.read
read v1 v2 ...
eg:
read firstname
echo ${firstname}
3.cat
-v 显示控制符
more
more|less
eg:
cat myfile1 myfile2 myfile3>myfile
cat -v dos.txt
cat ls.txt|more
cat ls.txt|less
less ls.txt
4.管道
命令1|命令2
df -k|awk '{print $1}'|grep -v "Filesystem"
5.tee
-a 不覆盖原有内容
df -k|awk '{print $1}'|grep -v "Filesystem"|tee -a out.txt
6.exec 替代当前shell
exec command 把现有shell关闭,重启新shell
文件描述符(0-2,系统使用,3-9可以用户字定义)
eg:
#!/bin/bash
#file_desc
exec 3<&0 0 <name.txt
read line1
read line2
exec 0<&3
echo $line1
echo $line2
7.文件重定向
command > filename 新文件(覆盖)
command >> filename 追加
command 1> filename 标准输出到文件中
command > filename 2>&1 把标准输出和标准错误一起重定向到一个文件中
command 2> filename 把标准错误重定向到文件中
command 2>> filename 追加
command >> filename 2>&1 追加
command < filename > filename2 输入 输出
command < filename 输入
command << delimiter 读入直至遇到delimiter
command < &m 把m作为标准输入
command > &m 把标准输出重定向到文件描述符m中
command < &- 关闭标准输入
eg:
1.cat >> term.txt << SIGANGJUN
2.grep "trident" missiles
grep "trident" missiles 2>/dev/null
3.cat account_new.txt account_old.txt\
1>account.out 2>accounts.err
4.grep "standard" standard.txt > grep.out 2>&1
5.
8.标准输入(0)、标准输出(1)和标准错误(2)
总共有12个,前三个是固定的
9.合并标准输出和标准错误
10.使用文件描述符
控制流结构
1.控制结构
流控制是什么?
make /home/sigangjun/shell/txt
cp *.txt /home/sigangjun/shell/txt/
rm -rf *.txt
上述问题出现问题怎么处理?
2.if then else语句
2.1 if 条件1
2.1.1
then
命令1
elif 条件2
then
命令2
else
命令3
fi
2.1.2
if条件
then 命令
fi
2.1.3 eg:
1).
if [ "10" -lt "12"]
then
echo "yes aslkdjfasld"
else
echo "no"
fi
2).
if cp mybifl.bak myfile;then
echo "aaaa"
else
echo "cccc"
fi
2.1.4 man test
2.1.5 vi 中 set nu
3.case语句
3.1
case 值 in
模式1)
命令
;;
模式2)
命令2
;;
esac
3.2 eg:
read ANS
case $ANS in
1)
echo "You select 1"
;;
2)
echo "You select 2"
;;
3)
echo "You select 3"
;;
y|Y)
echo "You select $ANS"
;;
*)
echo "`basename $0`:This is not between 1 and 3 ">&2
exit;
;;
esac
4.for循环
4.1
for 变量名 in 列表
do
命令1
命令2
done
4.2 eg:
for loop in 1 2 3 4 5
do
echo $loop
done
5.unitl循环
5.1
until 条件
do
命令1
命令2
done
5.2 eg:
Part="/backup"
LOOK_OUT=`df|grep "$Part"|awk '{print $5}|sed 's/%//g'`
echo $LOOK_OUT
until ["$LOOK_OUT" -gt "90"]
do
echo "Filesystem /backup is nearly full"|mail root
LOOK_OUT=`df|grep "$Part"|awk '{print $5}|sed 's/%//g'`
sleep 3600 #秒
done
6.while循环
while 命令
do
命令1
命令2
done
eg:
while echo -n "输入您喜欢的电影:";read FILM
do
echo "Yeah,${FILM}是一部好电影"
done
7.break控制
break[n] 退出循环
eg:
while :
do
echo -n "enter 1-5"
read ANS
case $ANS in
1|2|3|4|5)
echo "in 1-5"
;;
*)
echo "wrong"
breadk
;;
esac
done
8.continue控制
continue 跳过
eg:
while :
do
echo -n "enter 1-5"
read ANS
case $ANS in
1|2|3|4|5)
echo "in 1-5"
;;
*)
echo "wrong,is continue(y/n)?:"
read IS_CONTINUE
case $IS_CONTINUE in
y|yes|Y|Yes)
continue
;;
*)
break
;;
asac
esac
done