bash shell

1、
env //环境变量
set //自定义变量
export 变量名 //分享自己的变量设定给后来呼叫的文件或其他程序
read atest //读取输入为atest赋值
read -p “Please keyin your name: ” name //提示符
declare [-aixr] variable //a,数组;i,整数;x,环境变量;r,常量
declare -i sum=100+300+50 //定义变量类型
数组:
var[1]=1
var[2]=2
echo ${var[1]}
2、限制用户使用系统资源:
ulimit -a //列出所有限制
ulimit -f 10240 //限制建立文件大小在10MB以下
3、
(1)删除字符串:
${variable#/*local/bin:} // #代表从前面开始删除,*匹配任意字符串
echo ${path#/*:} // # :删除最短字符串
echo ${path##/*:} // ## :删除最长字符串
echo ${path%:*bin} //%:从后向前删除字符串
echo ${path%%:*bin} // %% 最长匹配
(2)替换字符串:
echo ${path/sbin/SBIN} //sbin替换为SBIN(替换一处)
echo ${path//sbin/SBIN} //替换所有
str="oldvar"; var=${str-newvar} ->var=str // - 不影响旧的字符串
str="oldvar"; var=${str=newvar} ->var=str
4、命令别名:
alias rm=’rm -i’
unalias lm
5、
列出历史命令:history 3
type 命令 //查看命令类型(shell内嵌、别名)
type -a echo //echo详细信息
cat /etc/issue //bash欢迎信息
\d 本地端时间的日期;
\l 第几个终端机接口
\m 硬件的等级
\r 操作系统的版本
\t 时间
\S 操作系统的名称;
\v 操作系统的版本。
/etc/issue.net //使用talnet连接主机时,调用issue.net
6、vim /etc/motd //所有终端登陆后接收信息
7、
(1)login shell :登陆后的shell
登陆后读取配置文件:/etc/profile ~/.bash_profile 、~/.bash_login 、 ~/.profile
login shell 读取流程:
这里写图片描述
(2)non-login shell :
读取配置文件:~/.bashrc
~/.bash_history :历史命令
~/.bash_logout : bash注销执行文件
8、stty -a //设置终端快捷键
9、通配符:
ll -d /etc/cron* //以cron开头的字符串
ll -d /etc/????? //有5个字符的字符串
ll -d /etc/*[0-9]* //含有数字的字符串
ll -d /etc/[^a-z]* //不含小写字母
10、数据流重导向:
将输出数据传输到其他文件或设备中
ll / > ~/rootfile //将ll / 输出到rootfile
ll / >> ~/rootfile //追加输出
> //正确信息
2> //错误信息
2>> //追加错误信息
find / -name testing >>list_right 2>>list_error //将正确信息输出到list_right ,错误信息输出到 list_error
find /home -name .bashrc 2> /dev/null // 丢弃错误数据,不会显示
find /home -name .bashrc > list 2>&1
find /home -name .bashrc &> list // 将正确、错误信息同时写入list中
cat > catfile :建立catfile文件
cat > catfile < ~/.bashrc :将.bashrc中的内容导入catfile中
11、
多命令执行:
cmd1;cmd2 //两条指令
cmd1&&cmd2 //cmd1 出现错误cmd2不会执行
cmd1||cmd2 //cmd1正确执行,cmd2不会执行
管线命令:
ls -al /etc | less //less处理前一个指令的输出
撷取命令:
echo $path | cut -d ':' -f 5 //截取第5段,以:为分隔符
echo $path | cut -d ':' -f 5,2 //截取第五段和第二段
last | grep ‘root’ //截取含root的信息
last | grep -v ‘root’ //截取不含root的信息
last | grep ‘root’ |cut -d ’ ’ -f1 // 截取第一栏
grep –color=auto ‘MANPATH’ /etc/man_db.conf //从文件中截取
排序:
cat /etc/passwd | sort -t ‘:’ -k 3 //以第三栏排序
last | cut -d ’ ’ -f1 | sort | uniq //重复数据显示一次
cat /etc/man_db.conf | wc //列出行,字数,字符数
tee:
ls -l /home | tee ~/homefile | more //tee 输出到屏幕和文件中
字符转换命令:
tr :删除、替换
col:将tab转换成空格
join:两个文件当中,有 “相同数据” 的那一行,将他加在一起
paste:将两行贴在一起,且中间以 [tab] 键隔开
expand:将 [tab] 按键转成空格键
cd /tmp; split -b 300k /etc/services services //将一个大文件分成若干300k的小文件
cat services* >> servicesback //合成
xargs : 读取stdin,并以空格分割其内容。使指令可作为管线命令。
cut -d ‘:’ -f 1 /etc/passwd | head -n 3 | xargs -n 1 id
tar -cvf - /home | tar -xvf - -C /tmp/homeback //数据流备份home到homeback
12、正则表达式:
dmesg | grep -n -A3 -B2 –color=auto ‘qxl’ //A3 B2 ,后3行,前2行
export LANG=C; export LC_ALL=C //设置本次登陆的语系
搜寻特定字符串:
grep -n ‘the’ regular_express.txt
grep -vn ‘the’ regular_express.txt //反向选择
grep -in ‘the’ regular_express.txt // 忽略大小写
grep -n ‘t[ae]st’ regular_express.txt //含有a或e
grep -n ‘oo’ regular_express.txt //含有oo的字符串
grep -n ‘[^g]oo’ regular_express.txt //oo前不含有g
grep -n ‘[^[:lower:]]oo’ regular_express.txt //oo前不能有小写字母
grep -n ‘^the’ regular_express.txt //行首是the
grep -n ‘.$’ regular_express.txt //以点结尾 (\ :消除特殊意义)
grep -n '^$' regular_express.txt //搜索空白行
grep -n ‘g..d’ regular_express.txt //点表示任意字符
grep -n ‘g.*g’ regular_express.txt // 任意多字符
grep -n ‘go{2,5}g’ regular_express.txt // 2~5个o
sed:
cat re.txt | sed ‘2,5d’ //删除2~5行
nl /etc/passwd | sed ‘3,$d’ //删除3~到最后一行
cat -n re.txt | sed ‘2a drink tea’ //第二行后加drink tea
nl /etc/passwd | sed ‘2,5c number’ //将2~5行用number替代
sed ‘s/要被取代的字符串/新的字符串/g’
awk:
last -n 5 | awk '{print $1 "\t" $3}' //取出第一列和第三列,以tab隔开
pr /etc/man_db.conf //打印时间、文档名、页码
13、shell script
使用bash、sh:script在子程序内执行
使用source:script在当前程序中执行
(1)特殊变量:
$0 : 文件名
$1:输入的第一个参数
$2:输入的第二个参数
$# :输入的参数的个数
$@、$*:显示所有参数

(2)shift偏移:
shift :去掉最前面一个参数
shift 3 :去掉最前面3个参数
(3)条件语句:
read -p “please input (y/n):” yn
if [ “${yn}” == “y” ];then // if [ ];then fi
echo “yes”
exit 0
fi // if结束

if [ “${yn}” == “n” ];then
echo “no”
exit 0
fi
多个条件:
if [ 条件1 ] ; then 语句1
elif [条件2] ; then 语句2
elif [条件3] ; then 语句3
…..
else 语句n
fi
探测111端口是否开启:

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

echo "now i will detect your port..."

touch ~/net.txt
file=~/net.txt
netstat -tuln > ${file}

test=$(grep ":111" ${file})

if [ "${test}" != "" ];then
     echo "111 port running."

fi

(4)case 语句:

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

case ${1} in
 "hello")
   echo "hello world"
  ;;
"")
 echo "you must input a para"
 ;;
*)  //相当于default
 echo "other"
 ;;
esac

(5)函数:
function fun(){
}
调用: fun;
$0 :函数名 $1: 第一个变量
function fun(){
echo $1
}
fun 1 //输出1
(6)循环:
while [ 条件]
do
语句
done //循环结束

until [ 条件] //条件成立,终止循环
do
语句
done //循环结束

for animal in dog cat elephant // in 常量/变量
do
echo ” ${animal}”
done
//dog cat elephant

for (( i=1; i<= n u ; i = i + 1 ) ) d o s = (( s + {i}))
done
(7)检查语法:
sh -n t.sh

猜你喜欢

转载自blog.csdn.net/misterfm/article/details/80537193