Linux学习笔记--Shell脚本
#!/bin/bash #非注释
使用参数传递内容
$1 $2 $3
使用命令进行交互
-read 读入数据给变量
-echo 输出数据
-printf 输出数据
例:
#!/bin/bash
read -p “please input your name: ” AAA
printf “$AAA is rich”
shell脚本执行
chmod a+x xx.sh
./xx.sh 打开子bash
root/xx.sh 也会打开子bash,变量要设置成环境变量,用export
bash ./xx.sh 会打开子bash
. ./xx.sh 不会打开子bash
判断
[ abc ] 没有空格表示任选一个
[ "$USER" = " root" ] && echo hello (&&:前面为true的话,继续执行后面)
[ -x /etc/passwd ] 是否有可执行权限
[ -f /usr/sbin/httpd ] 是否有这个进程
循环
if例
#!/bin/bash
if [ $AAA = shrek ]
then
echo hello shrek
elif [ $AAA = root]
then
echo hello root
else
echo get out
fi
bash -v(显示执行过程)x ./b
case例
#!/bin/bash
read AAA
case $AAA in
maxwell)
echo hello maxwell
;;
root)
echo hello root
;;
*)
echo get out
;;
esac
例 for
#!/bin/bash
for aaa in 111 222 333 /etc/profile.d/*.sh
do
echo $aaa
done
例while 当条件为true,开始循环
i=0
while [ $i -lt 100 ]
do
i=$ [ $i + 1 ]
echo -n “$1 ”
done
sed
sed -e(执行命令) ‘s(表示替换)/root/maxwell/’ /etc/passwd root被maxwell替换
sed -n(默认不输出) -e ‘/<maxwell>/s/bash/nologin/p(打印)’ /etc/passwd
sed -e ‘/^#/d(删除)’ -e ‘/^$/d’ /etc/squid/squid.conf
多次替换
sed -e ‘s/mike/todd/’ -e ‘s/good/better/’
sed ‘s/tiger/wolf/; s/after/before/’
sed -f sss /etc/passwd 执行脚本
awk
chkconfig –list | awk ‘$1==”httpd” { print $4}’
chkconfig –list | awk ‘BEGIN {III=0} {if ($5 == “3: on”) iii+=1} END{print iii}’
awk -F: ‘{print $1 “UID is” $3}’ /etc/passwd
浙公网安备 33010602011771号