发新话题
打印

[其他语言] 关于shell学习的一些建议和总结

关于shell学习的一些建议和总结

关于shell学习的一些建议和总结
     Author:NetSeek (注本文参照鸟哥的书的一个小小总结,感谢鸟哥)
Bash 判断执行的步骤:
1.如果读取到一个Enter符号(CR),就尝试开始执行该行命令.
2.如同前面Bash command提到的,指令间的多个空间会被忽略。
3.空白多行和Tab都会被忽略。
4.如果一行太多,可以用\延伸至下一行.
5.#后面的内容,被视为注释.

好习惯.
1.说明使用的shell为何.
2.注明shell的内容功能,版本信息,作者,文件创建日期.
3.添加每一个大步骤功能的注释.

eg:
#!/bin/bash
#Using for .....
#Written by NetSeek  From:http://www.linuxmine.com
#Date:2006/3/1

脚本执行的方法:
#chmod 755 scriptfile
#./scriptfile
或者: sh scriptfile

变量:
[root@www vbird_shells]# more hello.sh
#!/bin/bash
#Using for echo Hello World
#Date: 2006/3/1
#Written by:NetSeek
#From: http://www.linuxmine.com
i=Hello\ \!\ How\ are\ you\ \?
echo $i

[root@www vbird_shells]# cat var.sh
#!/bin/bash
#Using for study var
#Date:2006/3/1
#Written by: NetSeek
#From: http://www.linuxmine.com
name="NetSeek.Cao"
myname1=" My name is $name "
myname2='My name is $name'
echo $name
echo $myname1
echo $myname2
注:单引号里面的变量不会执行,不意变量如name="sss"不能空格写成name= "sss"会出错的!

卷标与运算符declare
declare 变量声明
#declare [-afirx]
-a 定义为数组 array
-f 定义为函数 function
-i 定义为整数 integer
-r 定义为只读
-x 定义为通过环境变量输出的变量

[root@www vbird_shells]# cat declare.sh
#!/bin/bash
#Using for study declare
#Written by: NetSeek Date:2006/3/1
#From: http://www.linuxmine.com
number1=2*3+5*6-24
declare -i number2=2*3+5*6-24
echo "Your result is ===> $number1"
echo "Your result is ===> $number2"


read 交换式shell的编写
[root@www vbird_shells]# cat read.sh
#!/bin/bash
#Using for study "read" variables
#Written by: NetSeek Date:2006/3/1
#From: http://www.linuxmine.com
echo "Please Enter your name, and  press Enter to start"
read name
echo "Your name is ====>$name"
echo "Hello $name, Do you love me?"


脚本参数代号:
#myscript   opt1   opt2   opt3  opt4
  $0         $1     $2     $3    $4
说明:
$0 :myscript,即脚本的文件名
$1 :opt1,即第一个附加参数
$2 :opt2
...
[root@www vbird_shells]# cat parm.sh
#!/bin/bash
#Using for study parameters
#Written by:NetSeek Date:2006/3/1
#From: http://www.linuxmine.com
echo "This script's nmae => $0"
echo "parameters $1 $2 $3"

[root@www vbird_shells]# sh parm.sh hello1 hello2 hello3
This script's nmae => parm.sh
parameters hello1 hello2 hello3

脚本的逻辑判断表达式
逻辑判断式:
关于文件目录:
-f      常用!检测文件是否存在
-d      常用!检测目录是否存在
-b      检测是否为一个block文件
-c      检测是否为一个character文件
-S      检测是否为一个socket标签文件
-L      检测是否为一个符号链接文件
-e      检测某个东西是否存在!可以是任何东西.

关于程序
-G      检测是否由GID所执行的程序拥有
-O      检测是否由UID所执行的程序拥有
-p      检测是否为程序间传送信息的name pipe 或FIFO

关于文件的属性的检测
-r      是否为可读
-w      是否为可写
-x      是否为可执行
-s      是否为非空白文件
-u      是否具有SUID属性
-g      是否具有SGID属性
-k      是否具有sticky bit 属性

两个文件之间的判断比较
-nt    第一个文件比第二个文件新
-ot    第一个文件比第二个文件旧
-ef    第一个文件与第二个文件为同一个文件(如链接文件)

与或非
&&     逻辑与
||     逻辑或

运算符:
=      等于
!=     不等于
<      小于
>      大于
-eq    等于
-ne    不等于
-lt    小于
-gt    大于
-le    小于或等于
-ge    大于或等于
-a     双方都成立(and)
-o     单方成立(or)
-z     空字符串
-n     非空字符串

条件判断:
if....then ....fi
语法:
if [ 条件一 ] &&(||) [ 条件二 ]; then
elif [ 条件三 ] &&(||) [ 条件四 ]; then

else
fi

[root@www vbird_shells]# cat ifthen.sh
#!/bin/bash
#Using for study if...then...fi
#Written by:NetSeek Date:2006/3/1
#From:http://www.linuxmine.com
echo "Press y to continue"
read yn
if [ "$yn" = "y" ]; then
        echo "scipt is running..."
else
        echo "stop!"
fi

case...esac
语法:
case 种类方式(string) in
      种类式一)
            程序执行段
            ;;
      种类式二)
            程序执行段
            ;;
        *)
          echo "Usage: {种类方式一|种类方式二}"
          exit 1
esac


[root@www vbird_shells]# cat case.sh
#!/bin/bash
#Using for study case...esac
#Wrtten by:NetSeek Date:2006/3/1
#From:http://www.linuxmine.com
echo "press your seclet loveme,hate,notknow"
read input
case $input in
        loveme)
              echo "Thinks! I will for my love for you too!"
              ;;
        hate)
              echo "I am so sad!  I hate you too :( !"
              ;;
        notknow)
              echo "Oh my god! You don't know?"
              ;;
        *)
              echo "Usage:{loveme|hate|notknow}"
              exit 1
esac

循环:
for,while,until
for:
for((条件1;条件2;条件3))
[root@www vbird_shells]# cat for.sh
#!/bin/bash
# Using for study for...
#Written by:NetSeek Date:2006/3/1
#From:http://www.linuxmine.com
declare -i s
for ((i=1; i<=100;i=i+1))
do
        s=s+i
done
echo "The count is ====> $s"

[root@www vbird_shells]# cat foruser.sh
#!/bin/bash
#Using for and loop to read the account of this linux server!
#Writen by: NetSeek Date:2006/3/1
#From:http://www.linuxmine.com
account=`cut -d ":" -f1 /etc/passwd |sort`
echo "The following is your linux server's account "
for i in $account
do
        echo $i
done

while:
[root@www vbird_shells]# cat while.sh
#!/bin/bash
#Using for while
#Written by:NetSeek Date:2006/3/1
#From:http://www.linuxmine.com
declare -i i
declare -i s
while [ "$i" != "101" ]
do
        s=s+i
        i=i+1
done
echo "The count is ====> $s"

until:
[root@www vbird_shells]# cat unitl.sh
#!/bin/bash
#Using for study until
#Written by:NetSeek Date:2006/3/1
#From:http://www.linuxmine.com
echo "Press Y/y to stop"
until [ "$yn" = "Y" ] || [ "$yn" = "y" ]
do
        read yn
done

如何调试脚本:
#sh [-nvx] scripts
-n :不执行脚本,查询脚本内的语法,若有错则列出
-v :在执行脚本之前,先将脚本的内容输出在屏幕上
-x :将用到的脚本内容显示在屏幕上,与-v稍有不同.



总结:多分析系统下/etc/init.d这里面的脚本,多分析网络上高手写的一些脚本。

[ 本帖最后由 neo 于 2007-8-9 13:26 编辑 ]
积极!努力!低调!百折不挠!IT运维专家网[LinuxTone]
分享才能进步!帮助他人就是在帮助自己! 共同学习探讨Linux!
  Linux= howto+man ! ^_^

TOP

支持一下,很感谢!

TOP

回复 #1 NetSeek 的帖子

顶顶,谢了
把握每一天

TOP

回复 #1 NetSeek 的帖子

很厉害哦,顶一下阿!

TOP

支持,我正打算学习这部分内容了!谢谢
人生不如意事常有八九,也许相爱多年的恋人会离你而去,也许一起相依相爱相濡以沫几十年的夫妻会反目,也许苦心经营几十年的事业堡垒会突然坍塌,也许一场飞来横祸会打得你措手不及……但,只要你心中还有梦想,你都能挺住!因为有时候梦想就是支撑我们生活下去的支柱!

TOP

呵呵。..哥们忘了.禁用Smilies
With great power comes great responsibility!

TOP

明天我上shell

TOP

发新话题