|
小弟今天开始学习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 |
|