QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1604|回复: 11

Shell String Operators test

[复制链接]
发表于 2005-1-25 20:47:52 | 显示全部楼层 |阅读模式
小弟今天开始学习bash shell 看到O'Reilly的《learning the bash shell》String Operators 一节
讲解${varname:-word},${varname:=word}
${varnamemessage},${varname:+word},${varnameffset:length}
这些东东的使用方法,看netman的《shell 13问》中也有这部分,看来蛮重要的
有点难搞懂,写了个测试的小脚本,做做练习.也算复习下了
脚本以一问一答的方式做练习。回答正确才会又笑脸哦。例如:
1: set VAR=linux .........
STR=${VAR:-free} $STR is :  linux
good! you are right ! ^_^
并且可以在做测试之前和之后查看《learning bash shell》中的解释
由于初学,用的方法笨,还望各位大侠多多指教 :D

#!/bin/bash
# Author : BlueSilence  E-MAIL: [email protected]
#This Shell script will tell you how to  use String Operators :
#${varname:-word},${varname:=word},${varnameffset}
#${varnamemessage},${varname:+word},${varnameffset:length}
#${variable#pattern} ${variable##pattern} ${variable%pattern}
#${variable%%pattern} ${variable/pattern/string}
#${variable//pattern/string}
# enjoy !
############# Set color commands, used via echo -e##########

purple="\\033[0;35m"
green="\\033[0;32m"
light_blue="\\033[1;34m"
NORMAL="\\033[0;39m"
###### ${varnamemessage} test ###################
selectVAR ()
{
FREE='VAR:free'
EXIT="VAR:freeAndAbortTheScript"
OPTIONS="null  $FREE  $EXIT linux "
select opt in $OPTIONS
do
    if [ "$opt" = "$EXIT" ]
    then
        echo 'excellent ! you are right ! ^_^ '
        return
    else
        echo 'wrong ! hurry up ! '
        return
     fi
done

}

selectVARII ()
{
FREE='VAR:free'
EXIT="VAR:freeAndAbortTheScript"
OPTIONS="null  $FREE  $EXIT linux "
select opt in $OPTIONS
do
    if [ "$opt" = "null" ]  || [ "$opt" = "linux" ]
    then
        echo 'excellent ! you are right ! ^_^ '
        return
    else
        echo 'wrong ! hurry up ! '
        return
     fi
done
}

##############explain menu#############################
explainSubstitution ()
{
PLUS='varname:+word'
MINUS='varname:-word'
EQUAL='varname:=word'
MESSAGE='varnamemessage'
OFFSET='varnameffset:length'
COLON='aboutColon('
OPTIONS=" $PLUS $MINUS $EQUAL $MESSAGE $OFFSET $COLON Return "
select opt in $OPTIONS
do
        case $opt in
                $PLUS)
                echo "$PLUS................"
                varname:+word
                ;;
                $MINUS)
                echo "$MINUS................ "
                varname:-word
                ;;
                $EQUAL)
                echo "$EQUAL................"
                varname:=word
                ;;
                $MESSAGE)
                echo "$MESSAGE..............."
                varnamemessage
                ;;
                $OFFSET)
                echo "$OFFSET................."
                varnameffset:length
                ;;
                $COLON)
                  echo "$COLON.................."
                  colon
                  ;;
                Return)
                  echo "Good Bye ! ^_^ "
                  echo "press enter to continue .........."
                  return
                  ;;
             esac
done
}
explainPattern ()
{
P1='variable#pattern'
P2='variable##pattern'
P3='variable%pattern'
P4='variable%%pattern'
P5='variable//pattern/string'
P6='aboutRemember'
OPTIONS="$P1 $P2 $P3 $P4 $P5 $P6 Return "
select opt in $OPTIONS
do
        case $opt in
                $P1)
                echo "$P1 ........."
                variable_pattern
                ;;
                $P2)
                echo "$P2 ........."
                variable_patternII
                ;;
                $P3)
                echo "$P3 ........."
                variable%pattern
                ;;
                $P4)
                echo "$P4 ........"
                variable%%pattern
                ;;
                $P5)
                echo "$P5 ........."
                variable//pattern/string
                ;;
                $P6)
                echo "$P6 ........."
                aboutRemember
                ;;
                Return)
                echo "Good Bye ! ^_^ "
                echo "press enter to continue ........"
                return
                ;;
        esac
done
}

explainALL ()
{
OPTIONS="SubstitutionOperatorsExplain PatternMatchExplain ReturnMenu"
        select opt in $OPTIONS
do
        case $opt in
                SubstitutionOperatorsExplain)
                echo "please wait ......."
                sleep 1
                explainSubstitution
                ;;
                PatternMatchExplain)
                echo "please wait ........"
                sleep 1
                explainPattern
                ;;
                ReturnMenu)
                echo "enjoy ! ^_^ "
                echo "press enter to continue ........."
                return
                  ;;
          esac
done
}

##############EXPLICATION  Content####################################
colon ()
{
    echo -e -n $NORMAL
    echo "The colon (  : "
    echo "The colon ( in all but the last of these operators is actually "
    echo "optional. If the colon is omitted, then change  exists and isn't "
    echo "null to \"exists\" in each definition, i.e., the operator tests  "
    echo "for existence only."
}

varname:-word ()
{
        echo -e -n $green
        echo -n '${varname:-word}  :    '
        echo "If varname exists and isn't null, return its value;"
        echo " otherwise return word."
        echo -n 'Purpose  :   '
        echo 'Returning a default value if the variable is undefined.'

}

varname:=word ()
{
        echo -e -n $light_blue
        echo -n ' ${varname:=word }  :   '
        echo 'If varname exists and isn't null, return its value;
        echo "otherwise set it to word and then return its value."
        echo " Positional and special parameters cannot be assigned this way."
        echo -n 'Purpose   :   '
        echo 'Setting a variable to a default value if it is undefined.'


}

varnamemessage ()
{
        echo -e -n $green
        echo -n '${varnamemessage} :   '
        echo "If varname exists and isn't null, return its value; "
        echo "otherwise print varname: followed by message "
        echo "and abort the current"
        echo "command or script (non-interactive shells only). Omitting message"
        echo 'produces the default message parameter null or not set.'
        echo -n 'Purpose :   '
        echo 'Catching errors that result from variables being undefined.'

}
varname:+word ()
{
      echo -e -n $purple
      echo -n '${varname:+word}  :   '
      echo "If varname exists and isn't null, return word; "
      echo "otherwise return null "
      echo -n 'Purpose  :    '
      echo 'Testing for the existence of a variable.'

}

varnameffset:length ()
{
         echo -e -n $purple
         echo  '${varnameffset:length}  :   '
         echo 'Performs substring expansion.a It returns the substring of '
         echo ' $varname starting at offset and up to length characters.'
         echo 'The first character in $varname is position 0. If length is '
         echo 'omitted, the substring starts at offset and continues to the '
         echo 'end of $varname. If offset is less than 0 then the position is '
         echo ' taken from the end of $varname. If varname is @, the length is '
         echo 'the number of positional parameters starting at parameter offset.'
         echo 'Purpose :  '
         echo "Returning parts of a string (substrings or slices)"
}

variable_pattern ()
{
        echo -e -n $purple
        echo -n '${variable#pattern}  :  '
        echo "If the pattern matches the beginning of the variable's value"
        echo "delete the shortest part that matches and return the rest"
}
variable_patternII ()
{
        echo -e -n $light_blue
        echo -n '${variable##pattern}  :  '
        echo "If the pattern matches the beginning of the variable's value"
        echo "delete the longest part that matches and return the rest."
}
variable%pattern ()
{
        echo -e -n $green
        echo -n '${variable%pattern}  :  '
        echo "If the pattern matches the end of the variable's value"
        echo "delete the shortest part that matches and return the rest"
}
variable%%pattern ()
{
        echo -e -n $NORMAL
        echo -n '${variable%%pattern}  :  '
        echo "If the pattern matches the end of the variable's value "
        echo "delete the longest part that matches and return the rest"
}
variable//pattern/string ()
{
        echo -e -n $light_blue
        echo  '${variable//pattern/string} : '
        echo "The longest match to pattern in variable is replaced by string"
        echo '${variable/pattern/string}: only the first match is replaced '
        echo '${variable//pattern/string}:  all matches are replaced '
        echo "If the pattern is begins with a # "
        echo "it must match at the start of the variable"
        echo "If it begins with a %, it must match with the end of the variable"
        echo "If string is null, the matches are deleted"
        echo "If variable is @ or * ,  the operation is applied to each "
        echo " positional parameter in turn and the expansion is "
        echo "the resultant list"
}
aboutRemember ()
{
        echo -e -n $NORMAL
        echo "These can be hard to remember, so here's a handy mnemonic device:"
        echo "# matches the front because number signs precede numbers"
        echo "% matches the rear because percent signs follow numbers"
        echo "When searching for the longest match "
        echo "use ## (because ## is longer than #)"
        echo " When searching for the shortest match, use # "
        echo "See, not that hard to remember at all! ^_^"
}



###########Substitution Operators test############################################
###${varname:-word}  ${varname:=word} ${varnamemessage} ${varname:+word}
###${varnameffset}  ${varnameffset:length}

substitution ()
{
echo -e -n $NORMAL
echo "1: set VAR=linux ........."
VAR=linux
sleep 1
echo -n 'ok... STR=${VAR:-free} $STR is :  '
read input
STR=${VAR:-free}
if [ "$input" = "$STR" ]
then
        echo 'good! you are right ! ^_^'
else
        echo 'wrong! hurry up ! '
fi
echo -n '$VAR is : '
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever ! ^_^ '
else
        echo 'wrong ! hurry up ! '
fi

echo  "2: set VAR=   (null)......."
VAR=
sleep 1
echo -n 'ok..... STR=${VAR:-free} $STR  is :  '
read input
STR=${VAR:-free}
if [ "$input" = "$STR" ]
then
        echo  'good ! you are right... ^_^'
else
        echo 'wrong ! hurry up ! '

fi
echo -n '$VAR is : '
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever  ! ^_^ '
else
        echo 'wrong ! hurry up '
fi

echo "3: set  VAR=    ......."
VAR=
sleep 1
echo -n 'ok..... STR=${VAR-free}  $STR  is :  '
read input
STR=${VAR-free}
if [ "$input" = "$STR" ]
then
        echo 'good ! you are right ^_^ '
else
        echo 'wrong ! hurry up ! '

fi
echo -n '$VAR is : '
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever ! ^_^'
else
        echo 'wrong ! hurry up ! '
fi

echo 'unset  VAR  ..........'
unset VAR
sleep 1
echo -n 'ok........  STR=${VAR-free}   $STR is : '
read input
STR=${VAR-free}
if [ "$input" = "$STR" ]
then
        echo 'good ! you are right ! ^_^ '
else
        echo 'wrong! hurry up ! '
fi
echo -n '$VAR is : '
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever  ! ^_^ '
else
        echo 'wrong ! hurry up ! '
fi


echo "4: set VAR=linux ......"
VAR=linux
sleep 1
echo -n 'ok....  STR=${VAR:=free} $STR  is :  '
read input
STR=${VAR:=free}
if [ "$input" = "$STR" ]
then
        echo 'good ! you are right ! ^_^'
else
        echo 'wrong ! hurry up ! :('
fi
echo -n "the VAR value is : "
read inputVar
if [ "$inputVar" = "$VAR" ]
then
        echo 'excellent ! so clever  !^_^ '
else
        echo 'wrong ! hurry up ! :('
fi
echo "5: set VAR=     ........ ."
VAR=
sleep 1
echo -n 'ok....  STR=${VAR:=free}  $STR  is : '
read input
STR=${VAR:=free}
if [ "$input" = "$STR" ]
then
        echo 'good ! you are right ! ^_^'
else
        echo 'wrong ! hurry up ! :( '

fi
echo -n '$VAR is : '
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever  ! ^_^'
else
        echo 'wrong ! hurry up ! :( '
fi

echo "6: set VAR=    ............"
VAR=
sleep 1
echo -n 'ok... STR=${VAR=free} $STR  is  '
read input
STR=${VAR=free}
if [ "$input" = "$STR" ]
then
        echo 'good ! you are right ! ^_^ '
else
        echo 'wrong ! hurry up ! :('
fi
echo -n "The VAR value is :  "
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever  ! ^_^'
else
        echo 'wrong ! hurry up ! :('
fi

echo 'unset VAR  ..........'
unset VAR
sleep 1
echo -n 'ok........  STR=${VAR=free}   $STR is : '
read input
STR=${VAR=free}
if [ "$input" = "$STR" ]
then
        echo 'good ! you are right ! ^_^ '
else
        echo 'wrong! hurry up ! :( '
fi
echo -n '$VAR is : '
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever  ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi


echo "7: set VAR=linux ..........."
VAR=linux
sleep 1
echo -n 'ok....  STR=${VAR:+free}  $STR  is '
read input
STR=${VAR:+free}
if [ "$input" = "$STR" ]
then
        echo 'good ! you are right ! ^_^'
else
        echo 'wrong ! hurry up ! :('
fi
echo -n "The VAR value is : "
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever  ! ^_^'
else
        echo 'wrong ! hurry up ! :('
fi
echo "8: set VAR=   ..........."
VAR=
sleep 1
echo -n 'ok..... STR=${VAR:+free}  $STR  is '
read input
STR=${VAR:+free}
if [ "$input" = "$STR" ]
then
        echo 'good ! you are right ! ^_^'
else
        echo 'wrong ! hurry up ! :( '
fi
echo -n "The VAR value is :"
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever  ! ^_^'
else
        echo 'wrong ! hurry up :( '
fi
echo "9: set VAR=   .........."
VAR=
sleep 1
echo -n 'ok..... STR=${VAR+free} $STR  is : '
read input
STR=${VAR+free}
if [ "$input" = "$STR" ]
then
        echo 'good ! you are right ! ^_^'
else
        echo 'wrong ! hurry up ! :('
fi
echo -n "The VAR value is : "
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever  ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi

echo 'unset VAR  ..........'
unset VAR
sleep 1
echo -n 'ok........  STR=${VAR+free}   $STR is : '
read input
STR=${VAR+free}
if [ "$input" = "$STR" ]
then
        echo 'good ! you are right ! ^_^ '
else
        echo 'wrong! hurry up ! :( '
fi
echo -n '$VAR is : '
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever  ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi

echo "10 : set VAR=linux ........"
VAR=linux
sleep 1
echo  'ok......  STR=${VARfree}   $STR is :'
selectVARII

echo "11 : set VAR=    ........."
VAR=
sleep 1
echo  'ok........  STR=${VARfree}  $STR is : '

selectVAR

echo "12 : set VAR=   .............."
VAR=
sleep 1
echo 'ok......... STR=${VAR?free}  $STR is : '
selectVARII


echo "13 :set VAR=Ilovelinux ......."
VAR=Ilovelinux
sleep 1
echo -n 'ok....  STR=${VAR:1:4} $STR is : '
read input
STR=${VAR:1:4}
if [ "$input" = "$STR" ]
then
        echo 'good ! you are right ! ^_^'
else
        echo 'wrong ! hurry up ! :( '
fi

echo -n "The VAR value is : "
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever  ! ^_^ '
else
        echo 'wrong ! hurry up ! :('
fi

echo "14 :set VAR=Ilovelinux ........"
VAR=Ilovelinux
sleep 1
echo -n 'ok.... STR=${VAR:4:5}  $STR  is : '
read input
STR=${VAR:4:5}
if [ "$input" = "$STR" ]
then
        echo 'excellent ! so clever !  ^_^'
else
        echo 'wrong ! hurry up ! :('
fi
echo "15 : set VAR=Ilovelinux ........."
VAR=Ilovelinux
sleep 1
echo -n 'ok......   STR=${VAR:1}   $STR  is : '
read input
STR=${VAR:1}
if [ "$input" = "$STR" ]
then
        echo 'good ! you are right ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi
echo -n '$VAR  is  : '
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi
echo "16 : set VAR=Ilovelinux ............."
VAR=Ilovelinux
sleep 1
echo -n 'ok.........  STR=${VAR:5}    $STR  is : '
read input
STR=${VAR:5}
if [ "$input" = "$STR" ]
then
        echo 'excellent ! so clever ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi
sleep 1
echo "complete............................."
echo "Are you crazy ? * ^_^*  "
echo "Explication : .........please select : "
explainSubstitution
}

##########pattenrns_match test###############################
####${variable#pattern} ${variable##pattern} ${variable%pattern}####
#####${variable%%pattern} ${variable/pattern/string} ${variable//pattern/string}

patterns_match ()
{
echo -e -n $NORMAL
echo "1: set VAR=/usr/bin/startkde  ........."
VAR=/usr/bin/startkde
sleep 1
echo -n 'ok....... STR=${VAR##*/}  $STR is : '
STR=${VAR##*/}
read inputSTR
if [ "$inputSTR" = "$STR" ]
then
         echo 'good ! you are right !  basename ? ^_^ '
else
         echo 'wrong ! hurry up ! :( '
fi

echo -n ' $VAR is  : '
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi

echo "2 : set VAR=/tmp/backup.tar.gz .........."
sleep 1
VAR=/tmp/backup.tar.gz
echo -n 'ok.....  STR=${VAR#*.}  $STR  is  : '
STR=${VAR#*.}
read inputSTR
if [ "$inputSTR" = "$STR" ]
then
        echo 'good ! you are right ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi

echo "3 : set VAR=/opt/gcc/bin/gcc ........"
VAR=/opt/gcc/bin/gcc
sleep 1
echo -n 'ok......  STR=${VAR%/*}  $STR  is  : '
STR=${VAR%/*}
read inputSTR
if [ "$inputSTR" = "$STR" ]
then
        echo 'good ! you are right ! dirname ? ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi
echo -n ' $VAR is : '
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi

echo "4 : set VAR=/tmp/backup.tar.gz   ........ "
VAR=/tmp/backup.tar.gz
sleep 1
echo -n 'ok......  STR=${VAR%%.*}   $STR  is  :  '
STR=${VAR%%.*}
read inputSTR
if [ "$inputSTR" = "$STR" ]
then
        echo 'good ! you are right ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi

echo "5 : set VAR=alicece  ........."
VAR=alicece
sleep 1
echo -n 'ok.......  STR=${VAR%%ce}  $STR  is : '
STR=${VAR%%ce}
read inputSTR
if [ "$inputSTR" = "$STR" ]
then
        echo 'excellent ! so clever ! because ce is exact match  ^_^ '
else
        echo 'wrong ! hurry up ! because ce is exact match so alice is right:( '
fi

echo "6 : set VAR=/bin:/usr/bin:/sbin ......... "
VAR=/bin:/usr/bin:/sbin
sleep 1
echo -n 'ok........  STR=${VAR//:/|}  $STR  is : '
STR=${VAR//:/|}
read inputSTR
if [ "$inputSTR" = "$STR" ]
then
        echo 'good ! you are right ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi
echo -n ' $VAR is  :  '
read inputVAR
if [ "$inputVAR" = "$VAR" ]
then
        echo 'excellent ! so clever ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi

echo "7 : set VAR=/tmp/backup.tar.gz  ........."
VAR=/tmp/backup.tar.gz
sleep 1
echo -n 'ok........ STR=${VAR/./..}  $STR is :  '
STR=${VAR/./..}
read inputSTR
if [ "$inputSTR" = "$STR"  ]
then
        echo 'good ! you are right ! ^_^ '
else
        echo 'wrong ! hurry up ! :( '
fi
echo "complete......................."
echo "EXPLANATION : "
sleep 1
explainPattern
}

###########main menu##############################
choice ()
{

    local OPTIONS=" Explanation SubstitutionOperatorsTest PatternMatchTest EXIT"
        select opt in $OPTIONS
        do
                case $opt in

                        Explanation)
                        echo "please wait......"
                        sleep 1
                        explainALL
                        ;;
                         SubstitutionOperatorsTest)
                         echo "please wait......"
                         sleep 1
                         substitution
                         ;;
                         PatternMatchTest)
                         echo "please wait......"
                         sleep 1
                         patterns_match
                         ;;
                         EXIT)
                         echo -e -n $NORMAL
                         echo "EXIT........"
                         exit
                         ;;
                 esac
        done

}

###########start here###############
echo -e -n $light_blue
echo "This Shell script will tell you how to use String Operators "
echo -n "String operators allow you to manipulate values of variables in various"
echo -n " useful ways without having to write full-blown programs or resort to "
echo -n "external UNIX utilities"
echo "In particular, string operators let you do the following:"
echo "*  Ensure that variables exist (i.e., are defined and have non-null values)"
echo "*  Set default values for variables"
echo "*  Catch errors that result from variables not being set"
echo "*  Remove portions of variables' values that match patterns"
sleep 2
echo "Are you Ready ?  Take a breath   three seconds ......... "
sleep 3
echo "ok  let\`s go ! ^_^"

echo "Now , What do you want ?"
   choice

#end
发表于 2005-1-26 01:10:34 | 显示全部楼层
very good  
回复

使用道具 举报

 楼主| 发表于 2005-1-26 15:35:30 | 显示全部楼层
加入了Pattern-Matching Operators 的练习 :
${variable#pattern},${variable##pattern}
${variable%pattern},${variable%%pattern}
${variable/pattern/string}
${variable//pattern/string

回复

使用道具 举报

发表于 2005-1-26 15:47:27 | 显示全部楼层
admin@Do admin $ unset UPDATEDB
admin@Do admin $ echo ${UPDATEDB:=updatedbdbdbdb}
updatedbdbdbdb
admin@Do admin $ echo ${UPDATEDB%%db}
updatedbdbdb
(为什么结果不是update??没有装后面所有的db 都删掉?)

* ${varname%%pattern} if pattern matches the end of the value of varname then delete the longest part that matches and return the rest
回复

使用道具 举报

 楼主| 发表于 2005-1-26 21:12:28 | 显示全部楼层
你要是做一下就知道了。。。
看这里。
echo "5 : set VAR=alicece ........."
VAR=alicece
sleep 1
echo -n 'ok....... STR=${VAR%%ce} $STR is : '
STR=${VAR%%ce}
read inputSTR
if [ "$inputSTR" = "$STR" ]
then
echo 'excellent ! so clever ! because ce is exact match ^_^ '
else
echo 'wrong ! hurry up ! because ce is exact match so alice is right '
fi
回复

使用道具 举报

发表于 2005-1-27 10:03:08 | 显示全部楼层
如果能下载脚本就好啦。。。

昨天看的时候有好多笑脸,我懒得转就找了网上的文档看了看
回复

使用道具 举报

发表于 2005-1-27 10:05:02 | 显示全部楼层
刚拷了一下个来,运行了一下,不知道在做什么,提示不明呀...
回复

使用道具 举报

 楼主| 发表于 2005-1-27 10:58:44 | 显示全部楼层
Now , What do you want ?
1) Explanation                3) PatternMatchTest
2) SubstitutionOperatorsTest  4) EXIT

选择一项
1) 《Learning bash shell 》中的解释
2) 做 字符串替换的练习 (varname:+word)
3) 做 分割匹配的练习  (variable#pattern)
例:
1: set VAR=linux .........
ok... STR=${VAR:-free} $STR is : linux  
good! you are right ! ^_^
$VAR is : linux
excellent ! so clever ! ^_^
.........
设置 VAR=linux  STR=${VAR:-free}
问 $STR 的值是什么 ? 回答linux  ,正确。
问 $VAR 的值是什么 ? 回答 linux ,正确
:D
回复

使用道具 举报

发表于 2005-1-27 19:11:07 | 显示全部楼层
#? admin@Do job $ ./StringOper.sh
./StringOper.sh: line 1: uthor: command not found
This Shell script will tell you how to use String Operators
String operators allow you to manipulate values of variables in various useful ways without having to write full-blown programs or resort to external UNIX utilitiesIn particular, string operators let you do the following:
* Ensure that variables exist (i.e., are defined and have non-null values)
* Set default values for variables
* Catch errors that result from variables not being set
* Remove portions of variables' values that match patterns
Are you Ready ? Take a breath three seconds .........
ok let`s go ! ^_^
Now , What do you want ?
1) Explanation                3) PatternMatchTest
2) SubstitutionOperatorsTest  4) EXIT
#? 1
please wait......
1) SubstitutionOperatorsExplain  3) ReturnMenu
2) PatternMatchExplain
#? 1
please wait .......
1) varname:+word          4) varnamemessage       7) Return
2) varname:-word          5) varnameffset:length
3) varname:=word          6) aboutColon(
#? 7
Good Bye ! ^_^
press enter to continue ..........
#?
1) SubstitutionOperatorsExplain  3) ReturnMenu
2) PatternMatchExplain
#? 2
please wait ........
1) variable#pattern          5) variable//pattern/string
2) variable##pattern         6) aboutRemember
3) variable%pattern          7) Return
4) variable%%pattern
#? 4
variable%%pattern ........
${variable%%pattern} : If the pattern matches the end of the variable's value
delete the longest part that matches and return the rest
#?

这儿下来我去做什么,我怎么测试呀?
回复

使用道具 举报

 楼主| 发表于 2005-1-27 19:21:05 | 显示全部楼层
Are you Ready ?  Take a breath   three seconds .........
ok  let`s go ! ^_^
Now , What do you want ?
1) Explanation                3) PatternMatchTest
2) SubstitutionOperatorsTest  4) EXIT
#? 3
please wait......
1: set VAR=/usr/bin/startkde  .........
ok....... STR=${VAR##*/}  $STR is : startkde
good ! you are right !  basename ? ^_^
$VAR is  : /usr/bin/startkde
excellent ! so clever ! ^_^
2 : set VAR=/tmp/backup.tar.gz ..........
ok.....  STR=${VAR#*.}  $STR  is  :                           
                     
     
Are you Ready ?  Take a breath   three seconds .........
ok  let`s go ! ^_^
Now , What do you want ?
1) Explanation                3) PatternMatchTest
2) SubstitutionOperatorsTest 4) EXIT
#? 2
please wait......
1: set VAR=linux .........
ok... STR=${VAR:-free} $STR is : linux
good! you are right ! ^_^
$VAR is : linux
excellent ! so clever ! ^_^
2: set VAR=   (null).......
ok..... STR=${VAR:-free} $STR  is :                        



你选择的都是对这些习题的解释说明。都是来自 《learning bash shell》的

做测试看我上面的选择 :D
另外出现
#?
提示符后,按下回车就可以看到选择菜单了。
回复

使用道具 举报

发表于 2005-1-27 19:41:28 | 显示全部楼层
中国电力出版社
回复

使用道具 举报

发表于 2005-1-28 09:30:11 | 显示全部楼层
hehe,so good..
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-10-6 15:26 , Processed in 0.055593 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表