for 循環
關於串行與並行執行
串行: 此時將按i的順序一步一步的執行
[root@dns ~]# for i in {1..253} ; do ping -t 1 -c 1 192.168.19.$i ; done
並行:將done之前的 ; 分號改爲& 表示後臺並行執行,如果人物之間沒有順序關係,並行提高速度
[root@dns ~]# for i in {1..253}; do ping -t 1 -c 1 192.168.19.$i & done
查看for的幫助
help for
沒有man手冊
for的第二種寫法
[root@dns ~]# for ((i=1;i<=10;i++))
> do
> echo $i
> done
> do
> echo $i
> done
獲取1-10之間的奇數
[root@dns ~]# for ((i=1;i<=10;i+=2)); do echo $i; done
或者取餘
[root@dns ~]# for ((i=1;i<=10;i+=2))
> do
> if [!`echo $(($i%2))`-eq0 ]
> then
> echo $i
> fi
> done
1
3
5
7
9
> do
> if [!`echo $(($i%2))`-eq0 ]
> then
> echo $i
> fi
> done
1
3
5
7
9
fork炸彈
man 2 fork
創建一個腳本自己創建自己,fork子進程,簡單的自己創建不足以造成系統無響應.
策略1 後臺並行執行加快fork速度,提高cpu佔用
策略2 指數創建進程 bash myself.sh | bash myself.sh $
策略3 執行較耗cpu的操作如:find命令
#!/bin/bash
echo $$
bash /root/homework1.sh| bash/root/homework1.sh&
if [ -d /tmp/abc ]
then
:
else
mkdir /tmp/abc
fi
x=1
for i in `find/usr/share/doc-name index.html`
do
cp $i /tmp/abc/index$((x++)).html
done
echo $$
bash /root/homework1.sh| bash/root/homework1.sh&
if [ -d /tmp/abc ]
then
:
else
mkdir /tmp/abc
fi
x=1
for i in `find/usr/share/doc-name index.html`
do
cp $i /tmp/abc/index$((x++)).html
done
結果:2秒內虛擬機掛了
用函數實現的fork bomb 函數名爲.
.(){ . | . &};.
while 循環
help while
while:while COMMANDS;do COMMANDS;
done
Expand and execute COMMANDS as long as the final commandin the
`while' COMMANDS has an exit status of zero.
Expand and execute COMMANDS as long as the final commandin the
`while' COMMANDS has an exit status of zero.
while 一般用來創建死循環做測試工作
[root@dns ~]# while true ; do echo ok; done
until循環
與while唯一的區別是,條件爲假的時候做循環
#!/bin/bash
x=1
until [ $x -ge 10 ]
do
echo $x
x=`expr $x+1`
done
x=1
while [ ! $x -ge 10 ]
do
echo $x
x=`expr $x+1`
done
x=1
until [ $x -ge 10 ]
do
echo $x
x=`expr $x+1`
done
x=1
while [ ! $x -ge 10 ]
do
echo $x
x=`expr $x+1`
done
select循環
[root@dns ~]# select i in "backup.sh" "restore.sh" "exit.sh"
> do
> $i
> done
1) backup.sh
2) restore.sh
3) exit.sh
#?
> do
> $i
> done
1) backup.sh
2) restore.sh
3) exit.sh
#?
PS" util 和select循環不經常用
退出循環條件
- break 退出循環執行循環後面的內容
- continue 退出一次循環,繼續執行剩下的循環
- exit 退出程序,循環後面的也不執行 exit n 表示退出狀態是n n爲整形數字
case
case: case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac
Selectively execute COMMANDS based upon WORD matching PATTERN. The
`|' is used to separate multiple patterns.
#!/bin/bash
case "$1" in
a)
echo "type a"
;;
b)
echo "type b"
;;
c)
echo "type c"
;;
*)
echo "no match"
esac
case "$1" in
a)
echo "type a"
;;
b)
echo "type b"
;;
c)
echo "type c"
;;
*)
echo "no match"
esac
系統服務啓動腳本如ftp都是使用case做選擇:實例如下:
#!/bin/bash
case "$1" in
start)
xeyes &
;;
stop)
pkill xeyes
;;
restart|reload)
pkill xeyes
xeyes &
;;
status)
if pidof xeyes &> /dev/null
then
echo "running"
else
echo "stopped"
fi
;;
*)
echo "Usage::$0 stop|start|restart|reload|status"
esac
case "$1" in
start)
xeyes &
;;
stop)
pkill xeyes
;;
restart|reload)
pkill xeyes
xeyes &
;;
status)
if pidof xeyes &> /dev/null
then
echo "running"
else
echo "stopped"
fi
;;
*)
echo "Usage::$0 stop|start|restart|reload|status"
esac
函數
function:
function NAME { COMMANDS ; } or NAME () { COMMANDS ; }
Create a simple command invoked by NAME which runs COMMANDS.
Arguments on the command line along with NAME are passed to the
function as $0 .. $n.
#!/bin/bash
function func1
{
echo "func1 body"
}
func()
{
echo "func body"
}
echo "ready to start func()"
func1
func
function func1
{
echo "func1 body"
}
func()
{
echo "func body"
}
echo "ready to start func()"
func1
func
函數版 fork bomb參看fork炸彈
數組
用小括號聲明:以空格區分不同的變量
declare a array(123 34 567 789)
echo ${array[*]}
echo ${array[@]}
HACK
如果想對當前終端產生影響 點空格 形式 :如修改PS1
組合重定向: &>/dev/null 或者
1>/dev/null ;2>1
shell調用函數時會創建一個新的子進程
BASH_BUILTINS man手冊中關於shell的用法解釋
NAME
bash, :, ., [, alias, bg, bind, break,
builtin, cd, command, compgen, complete, con-
tinue, declare, dirs, disown, echo, enable,
eval, exec, exit, export, fc, fg, getopts,
hash, help, history, jobs, kill, let, local,
logout, popd, printf, pushd, pwd, read, read-
only, return, set, shift, shopt, source, sus-
pend, test, times, trap, type, typeset,
ulimit, umask, unalias, unset, wait - bash
built-in commands, see bash(1)