bash 小记

原创 2012年03月29日 15:07:27
“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
newline is received.

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

相关文章推荐

c语言理论知识小记

  • 2017年06月15日 15:54
  • 11KB
  • 下载

matlab数学建模算法小记

  • 2013年04月20日 11:55
  • 4MB
  • 下载

Android PM常用命令使用小记

1. 包名信息查询 pm list packages [options][FILTER] -f 显示每个包的文件位置 -d 使用过滤器,只显示禁用的应用的包名 -e 使用过滤器,只...

javascript Dom开发小记

  • 2012年10月21日 00:07
  • 3.16MB
  • 下载

数据结构习题总结 拾荒小记

  • 2011年02月15日 20:55
  • 1.42MB
  • 下载

ThoughtWorks中国软件质量大会成都站小记

听说ThoughtWorks将于1月18日第一次在成都举办中国软件质量大会,周末闲着也没什么事,于是就报名参加了。虽然自己是做开发的,不过了解一些业界常用和前沿的软件测试以及质量维护的方法和技术也可以...

VC学习小记

  • 2011年11月18日 13:16
  • 68KB
  • 下载

阿里大数据分析平台使用小记

  • 2017年05月03日 17:28
  • 185KB
  • 下载

(2012.11.14-2013.01.17)六十四天的学习小记

(2012.11.14-2013.01.17)六十四天的学习小记     刷的一声,又过掉六十四天咯,还是那句老话,时间过得真快啊。回看上轮的计划任务,基本上是完成了,也不知道是可喜还是可悲了,完成...
  • neicole
  • neicole
  • 2013年01月17日 20:12
  • 5123

104规约规约学习小记

  • 2015年08月25日 13:54
  • 35KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:bash 小记
举报原因:
原因补充:

(最多只允许输入30个字)