Brief Introduction to Shell Script - 水阙 #14
Replies: 7 comments 1 reply
-
see: https://segmentfault.com/q/1010000002454596 放在>后面的&,表示重定向的目标不是一个文件,而是一个文件描述符,内置的文件描述符如下 1 => stdout 换言之 2>1 代表将stderr重定向到当前路径下文件名为1的regular file中,而2>&1代表将stderr重定向到文件描述符为1的文件(即/dev/stdout)中,这个文件就是stdout在file system中的映射 而&>file是一种特殊的用法,也可以写成>&file,二者的意思完全相同,都等价于
此处&>或者>&视作整体,分开没有单独的含义 第二个问题: find /etc -name .bashrc > list 2>&1 我想问为什么不能调下顺序,比如这样find /etc -name .bashrc 2>&1 > list 这个是从左到右有顺序的 第一种 xxx > list 2>&1 先将要输出到stdout的内容重定向到文件,此时文件list就是这个程序的stdout,再将stderr重定向到stdout,也就是文件list 第二种 xxx 2>&1 > list 先将要输出到stderr的内容重定向到stdout,此时会产生一个stdout的拷贝,作为程序的stderr,而程序原本要输出到stdout的内容,依然是对接在stdout原身上的,因此第二步重定向stdout,对stdout的拷贝不产生任何影响 |
Beta Was this translation helpful? Give feedback.
-
Bash中的整数运算参考:http://mywiki.wooledge.org/ArithmeticExpression 使用
Arithmetic CommandsBash提供两种命令使用math context,规则同 Exit status取决于math context的取值。如果取值为0,命令视为失败并且返回1,否则命令视为成功,返回0. 第一个算术命令是 let a=17+23
echo "a = $a" # prints a = 40 第二个算术命令是 ((a=$a+7)) # Add 7 to a
((a = a + 7)) # Add 7 to a. Identical to the previous command.
((a += 7)) # Add 7 to a. Identical to the previous command.
((a = RANDOM % 10 + 1)) # Choose a random number from 1 to 10.
# % is modulus, as in C. 在 if ((a > 5)); then echo "a is more than 5"; fi 这里有一点很容易困惑,算术命令
这一点和C语言恰恰相反,当执行一条bash指令(command),我们总有一个返回值(exit status),返回值取值范围从0到255. 0视为指令执行成功(当用于if或while条件判断时视为"true")。而在C语言中,0是false,其他都是true. 一些例子: true; echo "$?" # Writes 0, because a successful command returns 0.
((10 > 6)); echo "$?" # Also 0. An arithmetic command returns 0 for true.
echo "$((10 > 6))" # Writes 1. An arithmetic expression evaluates to 1 for true. 要弄清这一点,我们这样记。在
|
Beta Was this translation helpful? Give feedback.
-
几个选项的作用:
顺便一提,选项可以合并到一条set语句: |
Beta Was this translation helpful? Give feedback.
-
shell脚本条件表达式中的正则匹配x=clt
if [[ $x =~ ^\.?clt$ ]]; then
echo match
fi 匹配 |
Beta Was this translation helpful? Give feedback.
-
shell脚本写法参考,一条命令后面 git commit -m "Initial Commit" && {
git branch -D $BR;
git branch -m $BR;
git push -f origin $BR;
git gc --aggressive --prune=all
}; cf. https://gist.github.com/Zibri/76614988478a076bbe105545a16ee743 |
Beta Was this translation helpful? Give feedback.
-
脚本参数之间的区别:
cf. 此外,对于数组也是同理,仅在单个参数包含空格时有区别(目前所见),请看 array=(i love you "me too")
echo "the array@ is ${array[@]}"
echo "the array* is ${array[*]}"
echo "======== double-quoted@ ==========="
for x in "${array[@]}"; do echo $x; done
echo "======== double-quoted* ==========="
for x in "${array[*]}"; do echo $x; done
echo "=========== no-quote@ ============="
for x in ${array[@]}; do echo $x; done
echo "=========== no-quote*=============="
for x in ${array[*]}; do echo $x; done 输出为
什么?你还想问单引号括起来什么效果?单引号括起来压根不触发expand,直接当raw string输出。 |
Beta Was this translation helpful? Give feedback.
-
往文件里一次写入多行echo multiple lines into file cat >> /path/to/existingFile.text<< EOF
some text line 1
some text line 2
some text line 3
EOF 参考:https://stackoverflow.com/a/51864828 在脚本中回答问题有些情况下,命令会询问用户,等待用户输入。这在脚本中是不希望遇到的。可以这样依次回复脚本询问的问题。 command > /dev/null << EOF
<answer 1>
<answer 2>
<answer 3>
EOF |
Beta Was this translation helpful? Give feedback.
-
Brief Introduction to Shell Script - 水阙
This article is mainly refered to “The Linux Command Line”1. I just take some most important things out of the book.
Expansion Each time you type a command line and press the Enter key, bash performs several processes upon the text before it carries out your command. Just look an example:
1 2 [me@linuxbox ~]$ echo * Desktop Documents ls-output.txt Music Pictures Public Templates Videos Why not display an asterisk? That’s expansion!
https://blog.yychi.website/post/shell-intro/
Beta Was this translation helpful? Give feedback.
All reactions