Linux-系统管理

系统管理

tftp

nfs

samba

Shell编程

Shell是一个命令解析器

Shell 脚本是一个包含一系列命令序列的文本文件。

例:

#!/bin/sh
echo "hello world"
echo "hello!!"
mkdir /tnt

语法:

Shell脚本第一行必须是如下格式:

#!/bin/sh

符号#!用来指定该脚本文件的解析程序

当编辑好该脚本,还必须是其具有可执行属性

chmod +x filename

在进行shell编程时,以#开头的句子表示注释,知道这行结束

变量

在shell编程中,所有变量都由字符串组成,

并且不需要预先对变量进行声明,例:

#!bin/sh
#set variable a
a="hello world"
#print a
echo "A is :"
echo $a

注意定义时不要有空格

有时候容易与其他文字混淆,可以使用花括号

num=2
# 错误的表示,不会打印2nd
echo "this is $numnd"  
# 正确的表示,使用花括号
echo "this is the ${num}nd"

默认变量

$#:传入脚本的命令行参数个数
$*:所有命令行参数值,在各个参数值之间留有空格
$0:命令本身(shel文件名)
$1:第一个命令行参数
$2:第二个命令行参数

例:

#!/bin/sh
echo "number of vars:"$#
echo "value of vars:"$*
echo "value of var1:"$1
echo "value of var2:"$2

运行 ./test 1 2

输出结果

2
1 2
1
2

局部变量

在变量首次被赋值时加上local关键字可以声明一个局部变量

#!/bin/bash
hello="var1"

echo $hello

function func1 {

    local hello="var2"

    echo $hello

    }

func1

echo $hello

注意:

1.变量赋值时,“=”左右两边都不能有空格

2.BASH中的语句结尾不需要分号

if语句

if [expression]
then
    #code block
fi

if [expression]
then
    #code block
else
    #code block
fi

if [expression]
then
    #code block
else if [expression]
    then
        #code block
    else
        #code block
    fi
fi    

比较

比较操作 整数操作 字符串操作

相同 -eq =

不同 -ne !=

大于 -gt >

小于 -lt <

大于或等于 -ge

小于或等于 -le

为空 -z

不为空 -n

例:

比较整数a和b是否相等:if [ $a=$b ] (也可以用eq)

判断整数a是否大于整数b:if [ $a -gt $b ]

注意:1.在"["和"]"符号的左右都留有空格

2.“=”左右都由空格

实现对两个脚本参数的比较

#!/bin/bash
a=$1
b=$2
if [ -z $a ] || [ -z $b ]

then

    echo "please enter 2 no"

    exit 1

fi



if [ $a -eq $b ]

then

    echo "number a = number b"

else if [ $a -gt $b ]

    then

       echo "number a > number b"

    elif [ $a -lt $b ]

        then

            echo "number a < number b"

    fi
fi

判断

-e 文件已经存在

-f 文件是普通文件

-s 文件大小不为零

-d 文件是一个目录

-r 文件对当前用户可以读取

-w 文件对当前用户可以写入

-x 文件对当前用户可以执行

例:

#!/bin/sh
folder=/home
[ -r "$folder" ] && echo "Can read $folder"
[ -f "$folder" ] || echo "this is not file"

&& 左边为真执行后面的语句

|| 左边为假执行后面的语句

For循环

for var in [list]
do
    #code block
done

其中$var是循环控制变量 ,[list] 是var需要遍历的一个集合,do和done对包含循环体。

例:

#!/bin/bash
foe day in Sun Mon Tue Wed Thu Fri Sat
do
    echo $day
done

注意:在for那行,变量day是没有加“$”的,而在循环体内,echo所在的行变量$day是必须加上“$”符号的

练习:编写一个脚本统计当前目录的文件数

#!/bin/bash
a=0
for file in *
do
    a=`expr $a + 1`
done
echo "there are $a files in `pwd` need to process"

while循环

基本结构

while [ condition ]
do
    $code block
done

实验:编写脚本,将用户输入的数字按倒序的方式输出

#!/bin/bash

#input data
echo -n "Input number to reverse: "
read input

#define parameter
result=""
tmp=0
in=$input

#process
while [ $in -gt 0 ]
do
    tmp=$(($in % 10))
    in=$(($in/10))
    result=$(echo $result$tmp)
done

#print result
echo "$input in a reverse order is: $result" 

ps:不懂为什么要加两层(())

until循环

基本结构

until [ condition ]
do
    #code block
done

while和until的区别,while为真时执行,until为假时执行

实验:移动一个文件,如果目标文件存在该文件,则监视该文件,直到该文件被删除才移动该文件

#!/bin/bash
if [ "$1" = "" ] || [ "$2" = "" ]
then
    echo "Please enter file name"
    exit 1
fi
if [ -e $2 ]
then
    echo "The file already exists"
    until [ ! -f $2 ]
    do
        sleep 1
    done
fi
if [ ! `mv $1 $2` ]
then
    echo "mv sucessful"
else
    echo "mv error"
fi

Case语句

case "$var" in
condition 1 )
    ;;
condition 2 )
    ;;
*)
    default statments;;
esac

例:

#!#!/bin/bash
echo "Hit a key, then hit return"
read Keypress
case "$Keypress" in
    [A-Z] ) echo "Uppercase letter";;
    [a-z] ) echo "Lowercase letter";;
    [0-9] ) echo "Digit";;
    *) echo "Punctuation, whitespace, or other";;
esac

PS:程序有问题,仅a可以识别为小写,其他识别不了

原文地址:https://www.cnblogs.com/luoxiao23/p/11466028.html