ubuntu的sh文件编程(三)

来源:互联网 发布:程序员 计算器 编辑:程序博客网 时间:2024/06/15 19:09

中间运行有些需要加参数或者空格还有引号之类的,可能不能运行,下面是所有的测试代码:

#!/bin/bash#coding=utf-8echo "What is your name?"#read PERSON#echo "Hello,$PERSON"your_name="mozhiyan"echo $your_nameecho ${your_name}for skill in ada caffe action javadoecho "I am good at ${skill}script"donemyUrl="http://see.xidian.edu.cn/cpp/linux/"echo ${myUrl}#readonly myUrlmyUrl="http://see.xidian.edu.cn/cpp/shell/"echo ${myUrl}unset myUrlecho ${myUrl}echo $$echo ${$}echo "File Name: $0"echo "First Parameter : $1"echo "Second Parameter : $2"echo "Quoted Values: $@"echo "Quoted Values: $*"echo "Total Number of Parameters : $#"#"$* differ with $@ "echo "\$*=" $*echo "\"\$*\"=" "$*"echo "\$@=" $@echo "\"\$@\"=" "$@"echo "print each param from \$*"for var in $*do    echo "$var"doneecho "print each param from \$@"for var in $@do    echo "$var"doneecho "print each param from \"\$*\""for var in "$*"do    echo "$var"doneecho "print each param from \"\$@\""for var in "$@"do    echo "$var"doneecho $?a=10echo -e "Value of a is $a \n"DATE=`date`echo "Date is $DATE"USERS=`who | wc -l`echo "Logged in user are $USERS"UP=`date ; uptime`echo "Uptime is $UP"echo "+++++++++++++这段代码需要单独建立文件运行+++++++++++++++++++++"echo ${var:-"Variable is not set"}echo "1 - Value of var is ${var}"echo ${var:="Variable is not set"}echo "2 - Value of var is ${var}"unset varecho ${var:+"This is default value"}echo "3 - Value of var is $var"var="Prefix"echo ${var:+"This is default value"}echo "4 - Value of var is $var"echo ${var:?"Print this message"}echo "5 - Value of var is ${var}"echo "+++++++++++++以上这段代码需要单独建立文件运行+++++++++++++++++++++"val1=`expr 2 + 2`echo "Total value : $val1"a=10b=20val=`expr $a + $b`echo "a + b : $val"val=`expr $a - $b`echo "a - b : $val"val=`expr $a \* $b`echo "a * b : $val"val=`expr $b / $a`echo "b / a : $val"val=`expr $b % $a`echo "b % a : $val"if [ $a == $b ]then   echo "a is equal to b"fiif [ $a != $b ]then   echo "a is not equal to b"fia=10b=20if [ $a -eq $b ]then   echo "$a -eq $b : a is equal to b"else   echo "$a -eq $b: a is not equal to b"fiif [ $a -ne $b ]then   echo "$a -ne $b: a is not equal to b"else   echo "$a -ne $b : a is equal to b"fiif [ $a -gt $b ]then   echo "$a -gt $b: a is greater than b"else   echo "$a -gt $b: a is not greater than b"fiif [ $a -lt $b ]then   echo "$a -lt $b: a is less than b"else   echo "$a -lt $b: a is not less than b"fiif [ $a -ge $b ]then   echo "$a -ge $b: a is greater or  equal to b"else   echo "$a -ge $b: a is not greater or equal to b"fiif [ $a -le $b ]then   echo "$a -le $b: a is less or  equal to b"else   echo "$a -le $b: a is not less or equal to b"fia=10b=20if [ $a != $b ]then   echo "$a != $b : a is not equal to b"else   echo "$a != $b: a is equal to b"fiif [ $a -lt 100 -a $b -gt 15 ]then   echo "$a -lt 100 -a $b -gt 15 : returns true"else   echo "$a -lt 100 -a $b -gt 15 : returns false"fiif [ $a -lt 100 -o $b -gt 100 ]then   echo "$a -lt 100 -o $b -gt 100 : returns true"else   echo "$a -lt 100 -o $b -gt 100 : returns false"fiif [ $a -lt 5 -o $b -gt 100 ]then   echo "$a -lt 100 -o $b -gt 100 : returns true"else   echo "$a -lt 100 -o $b -gt 100 : returns false"fia="abc"b="efg"if [ $a = $b ]then   echo "$a = $b : a is equal to b"else   echo "$a = $b: a is not equal to b"fiif [ $a != $b ]then   echo "$a != $b : a is not equal to b"else   echo "$a != $b: a is equal to b"fiif [ -z $a ]then   echo "-z $a : string length is zero"else   echo "-z $a : string length is not zero"fiif [ -n $a ]then   echo "-n $a : string length is not zero"else   echo "-n $a : string length is zero"fiif [ $a ]then   echo "$a : string is not empty"else   echo "$a : string is empty"fifile="/home/x/git/sh_project/test.sh"if [ -r $file ]then   echo "File has read access"else   echo "File does not have read access"fiif [ -w $file ]then   echo "File has write permission"else   echo "File does not have write permission"fiif [ -x $file ]then   echo "File has execute permission"else   echo "File does not have execute permission"fiif [ -f $file ]then   echo "File is an ordinary file"else   echo "This is sepcial file"fiif [ -d $file ]then   echo "File is a directory"else   echo "This is not a directory"fiif [ -s $file ]then   echo "File size is zero"else   echo "File size is not zero"fiif [ -e $file ]then   echo "File exists"else   echo "File does not exist"fiyour_name='qinjx'str="Hello, I know your are \"$your_name\"! \n"echo -e $strNAME[0]="Zara"NAME[1]="Qadir"NAME[2]="Mahnaz"NAME[3]="Ayan"NAME[4]="Daisy"echo "First Index: ${NAME[0]}"echo "Second Index: ${NAME[1]}"NAME[0]="Zara"NAME[1]="Qadir"NAME[2]="Mahnaz"NAME[3]="Ayan"NAME[4]="Daisy"echo "First Method: ${NAME[*]}"echo "Second Method: ${NAME[@]}"length=${#NAME[*]}echo "$length"echo "\"It is a test\""mouth=8echo "${mouth}-1-2009"echo -e "OK!\n"echo "It is a test"echo -e "OK!\c"echo "It is a test"echo '$name\"'echo `date`printf "%d %s\n" 1 "abc"printf '%d %s\n' 1 "abc" printf "%s\n" abc defprintf "%s %s %s\n" a b c d e f g h i jprintf "%s and %d \n"printf "The first program always prints'%s,%d\n'" Hello Shellnum1=$[2*3]num2=$[1+5]if test $[num1] -eq $[num2]then  echo 'The two numbers are equal!'else  echo 'The two numbers are not equal!'fiecho 'Input a number between 1 to 4'echo -e 'Your number is:\c'read aNumcase $aNum in   1)  echo 'You select 1'        ;;   2)  echo 'You select 2'        ;;   3)  echo 'You select 3'        ;;   4)  echo 'You select 4'        ;;   *)  echo 'You do not select a number between 1 to 4'        ;;    esacoption="${1}"case ${option} in   -f) FILE="${2}"      echo "File name is $FILE"      ;;   -d) DIR="${2}"      echo "Dir name is $DIR"      ;;   *)       echo "`basename ${0}`:usage: [-f file] | [-d directory]"      exit 1 # Command to come out of the program with status 1      ;;esacfor loop in 1 2 3 4 5do  echo "The value is: $loop"donefor str in 'This is a string'do echo $strdonefor FILE in $HOME/.bash*do   echo $FILEdoneCOUNTER=0while [ $COUNTER -lt 5 ]do  COUNTER=`expr $COUNTER + 1`  echo $COUNTERdone#echo 'type <CTRL-D> to terminate'#echo -n 'enter your most liked film: '#while read FILM#do#  echo "Yeah! great film the $FILM"#donewhile :do    echo -n "Input a number between 1 to 5: "    read aNum    case $aNum in        1|2|3|4|5) echo "Your number is $aNum!"        ;;        *) echo "You do not select a number between 1 to 5, game is over!"            break        ;;    esacdonefor var1 in 1 2 3do   for var2 in 0 5   do      if [ $var1 -eq 2 -a $var2 -eq 0 ]      then echo "find the variables:$var1 $var2"         break 2      else         echo "$var1 $var2"      fi   donedoneNUMS="1 2 3 4 5 6 7"for NUM in $NUMSdo   Q=`expr $NUM % 2`   if [ $Q -eq 0 ]   then      echo "Number is an even number!!"      continue   fi   echo "Found odd number"doneHello () {   echo "Url is http://see.xidian.edu.cn/cpp/shell/"}HellofunWithReturn(){    echo "The function is to get the sum of two numbers..."    echo -n "Input first number: "    read aNum    echo -n "Input another number: "    read anotherNum    echo "The two numbers are $aNum and $anotherNum !"    return $(($aNum+$anotherNum))}funWithReturn# Capture value returnd by last commandret=$?echo "The sum of two numbers is $ret !"number_one () {   echo "Url_1 is http://see.xidian.edu.cn/cpp/shell/"   number_two}number_two () {   echo "Url_2 is http://see.xidian.edu.cn/cpp/u/xitong/"}number_onefunWithParam(){    echo "The value of the first parameter is $1 !"    echo "The value of the second parameter is $2 !"    echo "The value of the tenth parameter is $10 !"    echo "The value of the tenth parameter is ${10} !"    echo "The value of the eleventh parameter is ${11} !"    echo "The amount of the parameters is $# !"  # 参数个数    echo "The string of the parameters is $* !"  # 传递给函数的所有参数}funWithParam 1 2 3 4 5 6 7 8 9 34 73

有问题请留言。

0 0