# bash 小记

206人阅读 评论(0)
“A parameter is an entity that stores values.”
There are three types of parameters: positional parameters, special parameters, and variables.
Positional parameters are arguments present on the command line, and they are referenced by number.
Special parameters are set by the shell to store information about aspects of its current state, such as the number of arguments and the exit code of the last command. Their names are nonalphanumeric characters (for example, *, #, and _).
Variables are identified by a name.

The first two special parameters, $* and$@, expand to the value of all the positional parameters combined.
$# expands to the number of positional parameters.$0 contains the path to the currently running script or to the shell itself if no script is being executed.
 contains the process identification number (PID) of the current process,
$? is set to the exit code of the last-executed command, and$_ is set to the last argument to that command.
$! contains the PID of the last command executed in the background, and$- is set to the option flags currently in effect.

A variable is a parameter denoted by a name; a name is a word containing only letters, numbers, or underscores and beginning with a letter or an underscore.

AT&T’s System V and BSD. One of their differences was the behavior of echo. so use printf

stdout/stdin redirector：<,>>,>
FD0,FD1,FD2,
xxx > FILENAME1 2 > FILENAME2
xxx > FILENAME 2 > &1
& > FILENAME redirect the stdout and stderr.

The read command is a built-in shell that reads from the standard input. By default, it reads until a

Pipelines connect the standard output of one command directly to the standard input of another.

The tee command reads from the standard input and passes it to one or more files as well as to the
standard output.

The first, which originated in the Bourne shell, uses backticks:
date=date
The newer (and recommended) syntax is as follows:
date=$( date ) You can test the success of a command directly using the shell keywords while, until, and if or with the control operators && and ||. The exit code is stored in the special parameter$?.

Expressions are deemed to be true or false by the test command or one of two nonstandard shell
reserved words, [[ and ((. The test command compares strings, integers, and various file attributes;
(( tests arithmetic expressions, and [[ ... ]] does the same as test with the additional feature of
comparing regular expressions.

xxx@rh-ub063:/local/bash$type -a [ [ is a shell builtin [ is /usr/bin/[ xxx@rh-ub063:/local/bash$ type -a [[
[[ is a shell keyword

xxx@rh-ub063:/local/bash$test$(( $a - 2 )) -ne 0 xxx@rh-ub063:/local/bash$ echo $((8+12)) 20 xxx@rh-ub063:/local/bash$ echo $((8*12)) 96 Branch: printf "Enter a number between 10 and 20 inclusive: " read number if [ "$number" -lt 10 ]
then
printf "%d is too low\n" "$number" >&2 exit 1 elif [ "$number" -gt 20 ]
then
printf "%d is too high\n" "$number" >&2 exit 1 else printf "You entered %d\n" "$number"
fi

Lists containing the AND and OR conditional operators are evaluated from left to right. A command following the AND operator (&&) is executed if the previous command is successful. The part following the OR operator (||) is executed if the previous command fails.

case WORD in
PATTERN) COMMANDS ;;
PATTERN) COMMANDS ;; ## optional
esac

n=1
while [ $n -le 10 ] do echo "$n"
n=$(($n + 1 ))
done

n=1
until [ $n -gt 10 ] do echo "$n"
n=$(($n + 1 ))
done

for var in Canada USA Mexico
do
printf "%s\n" "$var" done for (( n=1; n<=10; ++n )) do echo "$n"
done

for n in a b c d e
do
while true
do
if [ $RANDOM -gt 20000 ] then printf . break 2 ## break out of both while and for loops elif [$RANDOM -lt 10000 ]
then
printf '"'
break ## break out of the while loop
fi
done
done
echo

for n in {1..9} ## See Brace expansion in Chapter 4
do
x=$RANDOM [$x -le 20000 ] && continue
echo "n=$n x=$x"
done
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：29636次
• 积分：583
• 等级：
• 排名：千里之外
• 原创：30篇
• 转载：6篇
• 译文：0篇
• 评论：1条
评论排行
最新评论