linxu下�.shå½ä»¤ç¸å½äºæ¯shellå½ä»¤è¯è¨ç解éå¨ãä¸é¢ç±æ为大家æ´çäºlinuxçshå½ä»¤ç详ç»è§£éçç¸å ³ç¥è¯ï¼å¸æ对大家æ帮å©!
ä¸ãLinuxä¸çshå½ä»¤ç详ç»è§£é
shå½ä»¤æ¯shellå½ä»¤è¯è¨è§£éå¨ï¼æ§è¡å½ä»¤ä»æ åè¾å ¥è¯»åæä»ä¸ä¸ªæ件ä¸è¯»åãéè¿ç¨æ·è¾å ¥å½ä»¤ï¼åå æ ¸è¿è¡æ²é!Bourne Again Shell (å³bash)æ¯èªç±è½¯ä»¶åºéä¼(GNU)å¼åçä¸ä¸ªShellï¼å®æ¯Linuxç³»ç»ä¸ä¸ä¸ªé»è®¤çShellãBashä¸ä½ä¸Bourne Shellå ¼å®¹ï¼è¿ç»§æ¿äºC ShellãKorn Shellçä¼ç¹ã
è¯æ³
bash [options] [file]
é项
-c stringï¼å½ä»¤ä»-cåçå符串读åã
-iï¼å®ç°èæ¬äº¤äºã
-nï¼è¿è¡shellèæ¬çè¯æ³æ£æ¥ã
-xï¼å®ç°shellèæ¬éæ¡è¯å¥çè·è¸ªã
äºãLinuxä¸çshå½ä»¤çå ·ä½ä¾å
使ç¨-xé项è·è¸ªèæ¬è°è¯shellèæ¬ï¼è½æå°åºææ§è¡çæ¯ä¸è¡å½ä»¤ä»¥åå½åç¶æï¼
[root@AY1307311912260196fcZ satools]# sh -x check_ssh_login.sh
+ DEFINE=30
+ cat /var/log/secure
+ awk '/Failed/ {++ip[$(NF-3)]} END {for (i in ip) print i"="ip[i]}'
++ cat /root/satools/black.txt
+ for i in '`cat /root/satools/black.txt`'
++ echo 121.42.0.16=1427
++ awk -F= '{print $1}' + IP=121.42.0.16
++ echo 121.42.0.16=1427
++ awk -F= '{print $2}'
+ NUM=1427
+ '[' 1427 -gt 30 ']'
+ grep 121.42.0.16 /etc/hosts.deny
+ '[' 1 -gt 0 ']'
+ echo sshd:121.42.0.16
+ echo vsftpd:121.42.0.16
+ for i in '`cat /root/satools/black.txt`'
++ echo 121.42.0.72=276
++ awk -F= '{print $1}'
+ IP=121.42.0.72
++ awk -F= '{print $2}'
++ echo 121.42.0.72=276
+ NUM=276 + '[' 276 -gt 30 ']'
+ grep 121.42.0.72 /etc/hosts.deny
+ '[' 1 -gt 0 ']'
+ echo sshd:121.42.0.72
+ echo vsftpd:121.42.0.72
ä¸ãLinuxä¸å¯¹.shæ件çæä½å½ä»¤
1ãå建test.shæ件
touch test.sh
2ãç¼è¾shæ件
vi test.sh
3ãä¿åéåº
æ²å»escï¼ ç¶åè¾å ¥ :wq ï¼å车éåº
4ãæ·»å å¯æ§è¡æéï¼å½ç¶é»è®¤å°±æ¯å¯æ§è¡çã
chmod +x test.sh
5ãè¿è¡æ件
(1)./test.sh
(2)sh test.sh
6ãå é¤æ件
rm test.sh
1�.shæ¯linuxä¸è¿è¡shellçå½ä»¤ï¼æ¯shellç解éå¨ï¼shellèæ¬æ¯linuxä¸å£³å±ä¸å½ä»¤è¡çé¢ï¼ç¨æ·å¯ä»¥å¨shellèæ¬è¾å ¥å½ä»¤æ¥æ§è¡åç§åæ ·çä»»å¡ã
è¦è¿è¡shellèæ¬ï¼é¦ééè¦ç»shellèæ¬æéï¼è¿éé以hello.shæ件为ä¾ï¼é¦å éè¦æä¸âcrtl+shift+Tâæå¼ç»ç«¯çªå£ï¼
2ãæ¥çå ç»âhello.shâæ件添å xæéchmod u+x hello.sh
3ãè¾å ¥âsh hello.shâå°±å¼å§æ§è¡shellèæ¬äºï¼æ¤æ¶å¨ç»ç«¯ä¸å°±è¾åºäºâhello!âçåæ ·ã以ä¸å°±æ¯ç¨shæ§è¡shellèæ¬çç®åæ¼ç¤ºï¼å½ç¶Shellæ¯ä¸ä¸ªåè½ç¸å½å¼ºå¤§çç¼ç¨è¯è¨ï¼æçæç¼åï¼æè°è¯ï¼çµæ´»æ§è¾å¼ºçç¹ç¹ï¼
不太难,要是你花点时间学, shell 编程对於系统管理员或是一般使用者都相当有用,除了自动化,还可写出一些有趣的小工具, 例如
我为自己写了个代替fortune 的小玩意,在 terminal 启动时执行这脚本,
它会连上 randomfunfacts.com, 随机地提供一些有趣的事实,和 vista
一个 gedget 一样,我在 .bash_profile 加入
# have some fun
if [ -x $(which funfacts) ]
then
funfacts | tee /etc/motd
else
fortune -s | tee /etc/motd
fi
而脚本放在 $HOME/bin
该脚本很简单,如是
#! /bin/bash
# get random fun facts from randomfunfacts.com, like `fortune'does.
# $prog: funfacts twfccc@gmail.com, twf_cc@yahoo.com.hk
# $required: lynx or links needed, gnu grep 2.5.X and bash 3.X or above
url="randomfunfacts.com" # the website
browser=lynx # default text browser
if which links &>/dev/null
then
browser=links # better choice if system provide
fi
if ! which lynx &>/dev/null &&! which links &>/dev/null
then
echo "lynx or links not found, sorry."
echo "Install one of them and execute script again."
exit 5
fi
case "$browser" in
links) facts="-dump $url | sed '/+-/,/+-/!d'"
lynx) facts="-dump $url | grep -A3 'U' | sed 1D"
esac
eval $browser $facts
exit 0
执行是这样
User@User-PC ~
$ funfacts
+------------------------------------------------------------------------------+
| An earthquake on Dec. 16, 1811 caused parts of the Mississippi River to flow |
| backwards! |
+------------------------------------------------------------------------------+
User@User-PC ~
$ funfacts
+------------------------------------------------------------------------------+
| An ostrich's eye is bigger than its brain. |
+------------------------------------------------------------------------------+
User@User-PC ~
$ !!
funfacts
+------------------------------------------------------------------------------+
| A crocodile cannot stick its tongue out. |
+------------------------------------------------------------------------------+
User@User-PC ~
$
也可用来写些实用工具,如到网站找最新代理服务器
$ cat bin/getproxy.sh
#! /bin/bash
# get update proxy list from www.proxy4free.com
# need gnu sed 4.1.X or above , bash 3.2.X or above
remotehost="www.proxy4free.com"
port=80
header="GET http://www.proxy4free.com/page1.html HTTP/1.0\n\n
Connection: Keep-Alive\n\n
Accept: text/html\n\n
Accept-Charest: iso-8859-1, *, utf-8\n\n
Accept-Language: en, zh, ja\n\n
Host: www.proxy4free.com:80\n\n
User-Agent: bash_script/0.1 [en, zh, ja] (Cygwin, NT)\n\n"
exec 5<>/dev/tcp/$remotehost/$port
printf "$header\n\n" >&5
sed -rn '/^<td>[0-9\.]+<\/td>$/p
/^<td>[0-9]+<\/td>$/p
/^<td>[a-z]+ ?[a-z]+<\/td>$/p
/^<td>[A-Z][a-z]+ ?[a-zA-Z]+<\/td>$/p
/^<td>20[0-1][0-9]-0?[1-9]+-0?[0-9]+<\/td>$/p' <&5 |
sed -r 's/^<td>(Name|IP|Port|Country|Type|Last Test)<\/td>$//g' |
sed '/^$/d' |
sed -r 's/<[^>]+>//g/20[0-1][0-9]-0?[1-9]+-0?[0-9]+/s//&\n----------------\n/g'
exec 5>&- 5<&-
awk 版本
User@User-PC ~
$ cat bin/getproxy.awk
#!/usr/bin/gawk -f
# get update proxy list from www.proxy4free.com
BEGIN{
header="GET http://www.proxy4free.com/page1.html HTTP/1.0\r\n\r\n \
Connection: Keep-Alive\n\r\n\r \
Accept: text/html\n\r\n\r \
Accept-Charest: iso-8859-1, *, utf-8\n\r\n\r \
Accept-Language: en, zh, ja\n\r\n\r \
Host: www.proxy4free.com:80\n\r\n\r \
User-Agent: gawk_script/0.99 [en, zh] (Cygwin, NT)\n\r\n\r"
host="www.proxy4free.com"
remotehost="/inet/tcp/0/" host "/80"
print header |&remotehost
while ((remotehost |&getline) >0){
sub(/^<td>(IP|Port|Country|Type|Last Test)<\/td>$/, "")
if ($0 ~ /^<td>[0-9\.]+<\/td>$/){
gsub(/<[^>]+>/, "")
print $0
} else if ($0 ~ /^<td>[0-9]+<\/td>$/){
gsub(/<[^>]+>/, "")
print $0
} else if ($0 ~ /^<td>[a-z]+ ?[a-z]+<\/td>$/){
gsub(/<[^>]+>/, "")
print $0
} else if ($0 ~ /^<td>[A-Z][a-z]+ ?[a-zA-Z]+<\/td>$/){
gsub(/<[^>]+>/, "")
print $0
} else if ($0 ~ /^<td>20[0-1][0-9]-0?[1-9]+-0?[0-9]+<\/td>$/){
gsub(/<[^>]+>/, "")
print $0
printf("\n----------------\n")
} else {
continue
}
}
close(remotehost)
}
shell 是用Linux 必学的一环,我应为用途很大,你看是不是? :)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)