编程笔记中提到的脚本

#!/bin/bash

#script1.sh参数变量

echo "脚本的名称是: $0"

echo "第一个位置: $1"

echo "第二个位置: $2"

echo "第三个位置: $3"

echo "第四个位置: $4"

echo "第五个位置: $5"

echo "第六个位置: $6"

echo "第七个位置: $7"

 

#!/bin/bash

#script2.sh位置变量示例

find /bin -name $1 -print

find /sbin -name $1 -print

 

#!/bin/bash

#script3.sh参数变量

echo "脚本的名称是: $0"

echo "第一个位置: $1"

echo "第二个位置: $2"

echo "第三个位置: $3"

echo "第四个位置: $4"

echo "第五个位置: $5"

echo "第六个位置: $6"

echo "第七个位置: $7"

echo "显示参数的个数: $#"

echo "显示脚本全部的参数: $*"

echo "显示进程ID: $$"

echo "显示前一命令i运行后的状态: $?"

 

#!/bin/bash

#script4.sh参数变量

echo "脚本的名称是: $0"

echo "第一个位置: $1"

echo "第二个位置: $2"

echo "第三个位置: $3"

echo "第四个位置: $4"

echo "第五个位置: $5"

echo "第六个位置: $6"

echo "第七个位置: $7"

echo "显示参数的个数: $#"

echo "显示脚本全部的参数: $*"

echo "显示进程ID: $$"

echo "显示前一命令i运行后的状态: $?"

shift 2

echo "脚本的第一个位置参数: $1"

echo "脚本的第二个位置参数: $2"

 

#!/bin/bash

#script5.sh $[]表达式形式举例

echo $[1+1]

echo $[2&5]

echo $[~5]

echo $[3^4]

echo $[3|4]

echo $[10#+1]

#上式中的#表示108进制数

 

#!/bin/bash

#script6.sh使用read函数

echo -n "First Name:"

read fname

echo -n "Last Name:"

read lname midname

echo -e "your firstname is:$fname"

echo -e "your lastname is:$lname"

echo -e "your middle name is:$midname /n"

 

#!/bin/bash

#script7.sh if语句的演示程序

if [ "10" -lt "12" ]

then

    #yes 10 is less than 12

    echo "yes,10 is less than 12"

else

    echo "NO"

fi

 

#!/bin/bash

#script8.sh test的程序演示

echo -n "Enter your name:"

read NAME

#user type her/his name and hit the enter key

if [ "$NAME" == "" ]

then

       echo "You didn't enter any information"

else

       echo "your name is $NAME"

fi

 

#!/bin/bash

#script9.sh if test 2

if cp file1 file1.bak

then

       echo "copy good"

else

       echo "`basename $0`:error could not copy the files">&2

fi

 

#!/bin/bash

#script10.sh elif

echo -e "Enter your name: /c"

read NAME

if [ -z $NAME ] || [ "$NAME" = ""]

then

       echo "you didn't enter any name"

elif [ "$NAME" = "root" ]

then

       echo "you are the root,hello"

elif [ "$NAME" = "chinaitlab" ]

then

       echo "you are chinaitlab"

else

       echo "you are not root or chinaitlab ,but hi $NAME"

fi      

 

#!/bin/sh

#script11.sh case示例,`basename $0`获得程序的名字

read ANS

case $ANS in

1)

  echo "you enter 1"

  ;;

2)

  echo "you enter 2"

  ;;

3)

  echo "you enter 3"

  ;;

*)

  echo "`basename $0`:this is not between 1 and3">&2

  exit

#exit有两个作用:一是停止程序中其他命令的执行,二是设置程序的退出状态

  ;;

Esac

 

#!/bin/bash

#script12.sh for循环

for loopint in 1 2 3 4 5

   do

          echo "$loopint"

   done

for loopchar in "hello nihao hi"

do

       echo "$loopchar"

done

for loopfile in "cat file"

do

       echo "$loopfile"

done

 

#!/bin/bash

#script13.sh until循环示例

part="/mnt/backup"

LOOK_OUT=`df|grep "$part" |awk '{print $5}' |sed 's/%//g'`

echo $LOOK_OUT

until ["$LOOK_OUT" -gt "90"]

do

       echo "filesystem $part is nearly full" |mail wcy@hbkj.com -s "disk is full"

done

 

#!/bin/bash

#script14.sh while read

while read LINE

do

       ehco $LINE

done < names.txt

 

#!/bin/bash

#script15.sh break and continue

while :

do

       echo -n "Enter any number [1...5]:"

       read ANS

       case $ANS in

              1|2|3|4|5)

              echo "you are enter a number between 1 and 5"

              ;;

              *)

              echo "wrong number,Bye"

              break

              ;;

       esac

done

 

#!/bin/bash

#script16.sh break and continue

while :

do

        echo -n "please inter a number between 1 and 5: "

        read line

        case $line in

                1|2|3|4|5)

                echo "you enter a number between 1 and 5"

                ;;

                *)

                while :

                do

                echo -n "wrong number continue (yes/no)"

                read judge

                case $judge in

                        y|yes|Y|YES)

                        continue 2

                        ;;

                        n|N|no|NO)

                        break 2

                        ;;

                        *)

                        continue

                        ;;

                esac

                done

        esac

done

 

#!/bin/bash

#script17.sh函数举例

#. function1调用functino1文件中的函数

. script18.sh

function hello ()

{

 DATE=`date |awk '{print $1}'`

#$1类似于位置变量,传递$user变量

 echo "hello $1 today is "$DATE"day"

 return 1

}

echo "now going to the function hello"

echo -n "please enter your name: "

read user

hello hi $user

echo "back from the function"

 

#!/bin/bash

#script18.sh

function hi ()

{

 echo "hi"

 return 1

}

 

#!/bin/bash

#script19.sh shift

function linenum ()

{

 echo "`basename $0`"

}

totalline=0

if [ $# -lt 2  ]

  then

  echo "at less 2"

  else

  while [ $# -ne 0 ]

    do

      line=`cat $1 |wc -l`

      echo "$1     $line"

      totaline=$[$totaline+$line]

      shift

    done

fi

echo "___________________________________"

echo "totalline is $totaline"

 

. script20.sh

hello

case $? in

   1)

     echo "it is 1"

     ;;

   2)

     echo "it is 2"

     ;;

   *)

     echo "error"

     ;;

esac

 

#!/bin/bash

#script21.sh function

function hello ()

{

 echo -n "please enter a number: "

 read num

 case $num in

 1)

 return 1

 ;;

 2)

 return 2

 ;;

 *)

 return err

 ;;

 esac

}

 

#!/bin/bash

#script22.sh getopts

YT="you are true"

YW="you are wrong"

ERR="there is something wrong"

HEL="hello"

# abc:d 参数,c后面加":"表示-c后面要输入内容

while getopts abc:d OPTION

       do

              case $OPTION in

                     a)

                     echo $YT

                     ;;

                     b)

                     echo $YW

                     ;;

                     c)

# $OPTARG是从 -c 参数后面接收来的内容

                     echo $OPTARG

                     echo $ERR

                     ;;

                     d)

                     echo $HEL

                     ;;

# /?表示所有其他未定义的参数,即无效参数

                     /?)

                     echo "valid options"

                     ;;

              esac

       done

#!/bin/bash
#script1 "<<"&"menu"
loopvar=2
main_menu ()
{
echo
echo
TITLE="My Menu"
TIMENOW=`date "+%Y-%m-%d %T"`
cat << file
          +----------------------------------+
      Title:    $title
      +----------------------------------+
      Date:     $TIMENOW
      +----------------------------------+
      |  ##   1) Show your name          |
      |  ##   2) Show your company       |
      |  ##   3) Show your home address  |
      |  ##   4) Show your telphone num  |
      |  ##   5) Help information        |
      |  ##   6) Exit                    |
      +----------------------------------+
file
}
while [ $loopvar -gt 0 ]
do
    main_menu
    echo -n "please select a number: "
    read num
    case $num in
        6)
        exit
        ;;
        *)
        clear
        continue
        ;;
    esac
done   

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值