您的位置:首页 > 运维架构 > Linux

【原创】linux sh脚本整理

2014-03-22 11:05 197 查看
一、开始

二、注释

三、#对变量赋值

四、 whlie read LINE 的特殊性

五、条件语句

六、脚本关系运算符(相等、大于、小于等)

七、文件应用命令(读行、读行数、)

附件一:处女作

一、开始

程序必须以下面的行开始(必须放在文件的第一行):

#!/bin/sh 或者 #!/bin/bash

  符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在这个例子中我们使用/bin/sh来执行程序.当编辑好脚本时,如果要执行该脚本,还必须使其可执行,要使脚本可执行:

编译 chmod +x filename 这样才能用./filename 来运行

二、注释

以#开头的句子表示注释

三、#对变量赋值:
!!!赋值等号两边不能随便加空格

#对变量赋值:

a="hello world"

array=($a) #表示将字符串“hello world” 赋给数组array,array以空格区分元素,故此array有两个元素;

length=${#array[@]} #此句可计算array 元素个数;



b=3

a=b+2 #此句结果为a的值为字符串"b+2"

a=$[$b+2] #词句结果为a的值为数值5

四、 whlie read LINE 的特殊性

lastnum=0

testnum=0

cat xxx.txt | whlie read LINE #词句表示逐行读取xxx.txt内容

do

if [ $LINE -qt $lastnum ]; then

echo "then"

testnum =2324

echo "testnum=$testnum ***"

else

echo "else"

fi

done

echo "testnum =$testnum ###"

打印结果:

testnum=2324***

testnum=2324***

testnum=2324***

``````

testnum=0###

由此结果说明:whlie read LINE 词句内的变量时局部变量,无法修改全局变量

如果将cat xxx.txt | whlie read LINE 改成相应的for 语句,就能在for循环内对全局变量进行赋值

五、条件语句

条件语句的特殊格式:

if [ $lastnum -lt $LINE ]; then #这里需要注意 扣号[ ] 前后需要有至少一个空格放大语句为:[ $lastnum -lt $LINE ]

六、脚本关系运算符(相等、大于、小于等)

1、数值

格式:

test "num1" opr "num2"

[ "num1" opr "num2" ]

opr 取值:

相等:-eq

不等:-ne

大于:-gt

小于:-lt 【l是字母L的小写】

小于等于:-le

大于等于:-ge

2、字符串

格式:

[ str1 opr str2]

[ opr str ]

opr取值:

相等:=

不等:!=

空串:-z

非空串:-n

3、文件

格式:

[ opr file ]

opr取值:

目录: -d

文件: -f

链接: -L

可读: -r

可写: -w

可执行: -x

文件非空: -s

4、逻辑运算符

逻辑与: -a 格式: [ condition1 -a condition2 ]

逻辑或: -o 格式: [ condition1 -o condition2 ]

逻辑否: ! 格式: [ ! condition ]

七、文件应用命令(读行、读行数、)

##一个命令的输出赋值给变量

num=$(sed -n "5p" xxx.txt)

##计算文件行数

line_chartxt=$(awk '{print NR}' $chardir|tail -n1) #$chardir 表示文件的路径

line_bustxt=$(awk '{print NR}' $busdir|tail -n1)

##读取文件某行值 (以第五行为例)

(sed -n "5p" xxx.txt)

附件一

处女作:

#!/bin/bash

##author : lwf

##date: 2014/3/21

##Feature: usb 端口重映射

usbmesgdir="/tmp/usbmesg.txt"

#lsusb -v > $usbmesgdir

busdir="/tmp/bus.txt"

chardir="/tmp/char.txt"

##获取hub,port相关信息分别保存到bus.txt;char.txt

##ID 0424:2514:TG3200 BUSid;0103:2108 特殊代号

grep 'ID 0424:2514' $usbmesgdir |cut -b7 > $busdir

grep '0103 power enable connect' $usbmesgdir |cut -b9 > $chardir

##计算文件行数

line_chartxt=$(awk '{print NR}' $chardir|tail -n1)

line_bustxt=$(awk '{print NR}' $busdir|tail -n1)

##数组初始化

state="0 0 0 0 0 0 0 0"

array=($state)

length=${#array[@]}

cnt=0

lastnum=0

## hub: hub1 hub2

##特殊情况(一种)port: 1 2 3 4

if [ $line_chartxt = 4 ] && [ $line_bustxt = 2 ]; then

array[$(sed -n "1p" $chardir)+($(sed -n "1p" $busdir)-1)*4-1]=1

array[$(sed -n "2p" $chardir)+($(sed -n "1p" $busdir)-1)*4-1]=1

array[$(sed -n "3p" $chardir)+($(sed -n "2p" $busdir)-1)*4-1]=1

array[$(sed -n "4p" $chardir)+($(sed -n "2p" $busdir)-1)*4-1]=1

##常规情况

elif [ $line_bustxt = 2 ];then

for((i=1; i<$[$line_chartxt+1]; i++))

do

LINE=$(sed -n "${i}p" $chardir)

if [ $lastnum -lt $LINE ]; then

array[$LINE+($(sed -n "2p" $busdir)-1)*4-1]=1

else

cnt=$[$cnt+1]

array[$LINE+($(sed -n "1p" $busdir)-1)*4-1]=1

fi

lastnum=$LINE

if [ $cnt -gt 0 ]; then

lastnum=5

fi

done

fi

##只有一条hub

if [ $line_bustxt = 1 ];then

for ((i=0; i<$length; i++))

do

array[$i]=0

done

for((i=1; i<$[$line_chartxt+1]; i++))

do

LINE=$(sed -n "${i}p" $chardir)

array[$LINE+($(sed -n "1p" $busdir)-1)*4-1]=1

done

fi

##测试结果 START###

#for ((i=0; i<$length; i++))

#do

# echo ${array[$i]}

#done

##测试结果 END###

rm $usbmesgdir -f

rm $busdir -f

rm $chardir -f

##end sh

基础运算:

cnt=0

cnt=$(expr $cnt + 1) #实现cnt加一的运算
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: