LEATH
1) basha=3 ; b=2 ; c=4if (( a > b )) && (( a < c ))或者if [[ $a > $b ]] && [[ $a < $c ]]或者if [ $a -gt $b -a $a -lt $c ]2) a=3 ; b=2 ; c=4if (( a > b )) || (( a < c ))或者if [[ $a > $b ]] || [[ $a < $c ]]或者if [ $a -gt $b -o $a -lt $c ]3) -o = or , -a = and , 但我一向只用 || 或者 &&4) 可用, 但是要两个独立的 [ ] , [[ ]] 或 (( ))看 1)5) -ne 比较数字 (numberic) ; != 比较字符 (string), 但后者拿来比较数字也可,只是不是标准用法-lt 是等同 < , 但 < 只能在 shell 的数值操作符 (( )) 或者 逻缉操作符 [[ ]] 才可使用, -lt , -eq , -gt , -ge-le , 这些是 test , 就是 [ ] 这个内建命令使用的条件操作符, 数字用, = , != 字符用, == 这个该是 [[ ]] 用的,可用来比对正规表示式, 但用在 [ ] 也可,只是不太正统用法
喵喵时光机
不同的shell 有不同的写法,以下是各个shell的比较:FeatureC/TCBourneBashKornVariables:Assigning values to local variablesset x = 5x=5x=5x=5Assigning variable attributesdeclare ortypesettypesetAssigning values to environment variablessetenv NAME BobNAME='Bob'; export NAMEexport NAME='Bob'export NAME='Bob'Read-Only Variables:Accessing variablesecho $NAMEset var = netecho ${var}worknetworkecho $NAMEvar=netecho ${var}worknetworkecho $NAMEvar=netecho ${var}worknetworkecho $NAME or print $NAMEvar=netprint ${var}worknetworkNumber of charactersecho $%var (tcsh only)N/A${#var}${#var}Special Variables:PID of the process$$$$$$$$Exit status$status, $?$?$?$?Last background job$! (tcsh only)$!$!$!Arrays:Assigning arraysset x = ( a b c )N/Ay[0]='a'; y[2]='b'; y[2]='c'fruit=(apples pears peaches plums)y[0]='a'; y[1]='b'; y[2]='c'set –A fruit apples pears plumsAccessing array elementsecho $x[1] $x[2]N/Aecho ${y[0]} ${y[1]}print ${y[0]} ${y[1]}All elementsecho $x or $x[*]N/Aecho ${y[*]}, ${fruit[0]}print ${y[*]}, ${fruit[0]}No. of elementsecho $#xN/Aecho $y{#[*]}print ${#y[*]}Command Substitution:Assigning output of command to variableset d = `date`d=`date`d=$(date) or d=`date`d=$(date) or d=`date`Accessing valuesecho $decho $d[1], $d[2],...echo $#decho $decho $dprint $dCommand Line Arguments (Positional Parameters):Accessing$argv[1], $argv[2] or$1, $2 ...$1, $2 ... $9$1, $2, ... ${10} ...$1, $2, ... ${10} ...Setting positional parametersN/Aset a b cset `date`echo $1 $2 ...set a b cset `date` or set $(date)echo $1 $2 ...set a b cset `date` or set $(date)print $1 $2 ...No. of command line arguments$#argv$# (tcsh)$#$#$#No. of characters in $arg[number]$%1, $%2, (tcsh)N/AN/AN/AMetacharacters for Filename Expansion:Matches for:Single character????Zero or more characters****One character from a set[abc][abc][abc][abc]One character from a range of characters in a set[a–c][a–c][a-c][a–c]One character not in the setN/A (csh)[^abc] (tcsh)[!abc][!abc][!abc]? matches zero or one occurrences of any pattern in the parentheses. The vertical bar represents an OR condition; e.g., either 2 or 9. Matches abc21, abc91, or abc1.abc?(2|9)1abc?(2|9)1Filenames not matching a pattern^pattern (tcsh)I/O Redirection and Pipes:Command output redirected to a filecmd > filecmd > filecmd > filecmd > fileCommand output redirected and appended to a filecmd >> filecmd >> filecmd >> filecmd >> fileCommand input redirected from a filecmd < filecmd < filecmd < filecmd < fileCommand errors redirected to a file(cmd > /dev/tty)>&errorscmd 2>errorscmd 2> filecmd 2> errorsOutput and errors redirected to a filecmd >& filecmd > file 2>&1cmd >& file or cmd &> file or cmd > file 2>&1cmd > file 2>&1Assign output and ignore noclobbercmd >| fileN/Acmd >| filecmd >| filehere documentcmd << EOFinputEOFcmd << EOFinputEOFcmd << EOFinputEOFcmd << EOFinputEOFPipe output of one command to input of another commandcmd | cmdcmd | cmdcmd | cmdcmd | cmdPipe output and error to a commandcmd |& cmdN/AN/A(See coprocesses)CoprocessN/AN/AN/Acommand |&Conditional statementcmd && cmdcmd || cmdcmd && cmdcmd || cmdcmd && cmdcmd || cmdcmd && cmdcmd || cmdReading from the Keyboard:Read a line of input and store into variable(s)set var = $<set var = 'line'read varread var1 var2...read varread var1 var2...readread -p promptread -a arraynameread varread var1 var2...readread var?"Enter value"Arithmetic:Perform calculation@ var = 5 + 1var=`expr 5 + 1`(( var = 5 + 1 ))let var=5+1(( var = 5 + 1 ))let var=5+1Tilde Expansion:Represent home directory of user~usernameN/A~username~usernameRepresent home directory~N/A~~Represent present working directoryN/AN/A~+~+Represent previous working directoryN/AN/A~-~–Aliases:Create an aliasalias m moreN/Aalias m=morealias m=moreList aliasesaliasalias, alias -palias, alias –tRemove an aliasunalias mN/Aunalias munalias mHistory:Set historyset history = 25N/Aautomatic or HISTSIZE=25automatic or HISTSIZE=25Display numbered history listhistoryhistory, fc -lhistory, fc –lDisplay portion of list selected by numberhistory 5history 5history 5 10history –5Re-execute a command!! (last command)!5 (5th command)!v (last command starting with v)!! (last command)!5 (5th command)!v (last command starting with v)r (last command)r5 (5th command)r v (last command starting with v)Set interactive editorN/A (csh)bindkey -vor bindkey -e (tcsh)N/Aset -o viset -o emacsset -o viset -o emacsSignals:CommandonintrtraptraptrapInitialization Files:Executed at login.login.profile.bash_profile.profileExecuted every time the shell is invoked.cshrcN/ABASH_ENV=.bashrc (or other filename) (bash 2.x)ENV=.bashrcENV=.kshrc (or other filename)Functions:Define a functionN/Afun() { commands; }function fun { commands; }function fun { commands; }Call a functionN/Afunfun param1 param2 ...funfun param1 param2 ...funfun param1 param2 ...Programming Constructs:if conditionalif ( expression ) thencommandsendifif { ( command ) } thencommandsendifif [ expression ]thencommandsfiif commandthencommandsfiif [[ string expression ]]thencommandsfiif (( numeric expression ))thencommandsfiif [[ string expression ]]thencommandsfiif (( numeric expression ))thencommandsfiif/else conditionalif ( expression ) thencommandselsecommandsendifif commandthencommandselse...fiif commandthencommandselse...fiif commandthencommandselse...fiif/else/elseif conditionalif (expression) thencommandselse if (expression) thencommandselsecommandsendifif commandthencommandselif commandthencommandselsecommandsfiif commandthencommandselif commandthencommandselsecommandsfiif commandthencommandselif commandthencommandselsecommandsfigotogoto label...label:N/AN/AN/Aswitch and caseswitch ("$value")case pattern1:commandsbreakswcase pattern2:commandsbreakswdefault:commandsbreakswendswcase "$value" inpattern1) commands;;pattern2) commands;;*) commands;;esaccase "$value" inpattern1) commands;;pattern2) commands;;*) commands;;esaccase "$value" inpattern1) commands;;pattern2) commands;;*) commands;;esacLoops:whilewhile (expression)commandsendwhile commanddocommanddonewhile commanddocommanddonewhile commanddocommandsdonefor/foreachforeach var (wordlist)commandsendfor var in wordlistdocommandsdonefor var in wordlistdocommandsdonefor var in wordlistdocommandsdoneuntilN/Auntil commanddocommandsdoneuntil commanddocommandsdoneuntil commanddocommandsdonerepeatrepeat 3 "echo hello"hellohellohelloN/AN/AN/AselectN/AN/APS3="Please select a menu item"select var in wordlistdocommandsdone