linux shell编程入门

S1:打印一个变量a

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

#/bin/sh     

#设置一个变量a,不需要预先对变量经行声明

a=”hello world”        #赋值时,=左右两边都不能有空格

#打印变量a

echo “A is :”

echo $a     

shell脚本编程入门知识

num=2

echo “this is the $numnd

打印出“this is the

num=2

echo “this is the ${num}nd”

打印出“this is the 2nd 

S2:默认变量$#$*$1$2……的使用

#!/bin/sh

echo "number of vars:"$#       #  $#:传入脚本的命令行参数个数

echo "values of vars:"$*        #  $*:所有命令行参数值,在各个参数值之间有空格

echo "value of var1:"$1        #  $1:第一个命令行参数值

echo "value of var2:"$2

echo "value of var3:"$3

echo "value of var4:"$4 

shell脚本编程入门知识

s3:局部变量的使用

#!/bin/bash

hello="var1"

echo $hello

function func1 {

       local hello="var2"       #  local 关键字可以声明一个局部变量

       echo $hello

       }

       func1

echo $hello

shell脚本编程入门知识

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

#!/bin/bash

a=$1                    #  将第一个命令行参数值赋值给变量a

b=$2

if [ -z $a ] || [ -z $b ]       # -z 判断两个变量是否有一个为空

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 ]                  # -lt”左右都有空格

        then

            echo "number a<number b"

    fi

fi

shell脚本编程入门知识

s5:文件测试操作符使用

#!/bin/sh

folder=/home

[ -r "$folder" ] && echo "Can read $folder"  # 如果folder可读,则执行后面命令

[ -f "$folder" ] || echo "this is not file"     #测试为假,则执行后面命令。普通文件

shell脚本编程入门知识 

s6:根据输入的参数值,创建一个名字为参数值的文件夹

#!/bin/bash

DIR=$1                   

#if  the string empty

if [ "$DIR" = "" ]

then

    echo "usage: `basename $0` directory to create" >&2

    exit 1

fi

echo "dir" $DIR

if [ -d $DIR ]               # 测试DIR是否为目录

then

    echo "The directory already exist"

    exit 0

else

    echo "The directory does not exist"

    echo -n "Create is now? [Y/N]:"       # -n 取消换行

    read create                       # read命令接受用户的输入

    if [ "$create" = "y" ] || [ "$create" = "Y" ] 

    then

        echo "creating now"

        if [ ! `mkdir $DIR` ]    # 判断创建目录是否成功

                 then DIR=" "

            fi   

   

            if [ "$DIR" = " " ]

           then

                     echo "create directory sucess"

        else

            echo "create directory error"

        fi

    elif [ "$create" = "n" ] || [ "$create" = "N" ]

           then

        echo "does not create directory"

        exit 0

    else

        echo "Errors order"

        exit 1

    fi

fi

shell脚本编程入门知识 

s7:循环结构的使用1

#!/bin/bash

for day in Sun Mon Tue Wed Thu Fri Sat

do

       echo $day

done

shell脚本编程入门知识 

s8:循环结构的使用2

#!/bin/bash

for day in "Sun Mon Tue Wed Thu Fri Sat" 

do                             # 列表包含在一对双引号之中,被认为一个元素

       echo $day

done

shell脚本编程入门知识 

s9:统计当前目录下的文件数

#!/bin/bash

counter=0

for files in *                    # 替换符*表明对当前目录中所有文件循环

do

    counter=`expr $counter + 1`   #  expr:对表达式求值的命令

done                         # let counter=$counter+1

echo "There are $counter files in `pwd` we need to process"

shell脚本编程入门知识 

s10:将用户输入的数字按倒序输出

#!/bin/bash

echo -n "Pleasw enter number : "

read n

sd=0

rev=""

on=$n

echo "$n"

while [ $n -gt 0 ]

do

    sd=$(( $n % 10 ))           # get Remainder

    n=$(( $n / 10 ))            # get next digit

    rev=$( echo $rev$sd)       #返回记号可以取得指定变量中的内容

done

echo  "$on in a reverse order $rev"

shell脚本编程入门知识 

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

#!/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

shell脚本编程入门知识

s12:根据输入的字符来判断是大写字母还是小写字母还是数字或是其他

#!/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

shell脚本编程入门知识 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值