learning bash shell 学习笔记(三)
来源:互联网 发布:linux --help和help 编辑:程序博客网 时间:2024/05/22 03:23
1.递归脚本
For example, consider this script, tracedir, in your home directory:
file=$1echo $file if [ -d "$file" ]; then cd $file ~/tracedir $(ls) cd ..fi
This script has a few problems, but it is the basis for the solution to this task.
One major problem with the script is that it is very inefficient. Each time the script is called, a new shell is created.
We can improve on this by making the script into a function, because (as you probably remember from Chapter 4) functions are part of the shell they are started from.
recdir ( ){ tab=$tab$singletab for file in "$@"; do echo -e $tab$file thisfile=$thisfile/$file if [ -d "$thisfile" ]; then recdir $(command ls $thisfile) fi thisfile=${thisfile%/*} done tab=${tab%"$singletab"}}
改进
recdir ( ){ tab=$tab$singletab for file in "$@"; do echo -e $tab$file thisfile=$thisfile/$file if [ -d "$thisfile" ]; then recdir $(command ls $thisfile) fi thisfile=${thisfile%/*} done tab=${tab%"$singletab"}}
2.
grep 中*代表匹配零个或多个先前字符
比如grep '^-[0-9]*$' 代表以-开头的行,后面跟一个或任意个0到9的数字结尾
所以表示an initial dash followed by a digit, optionally followed by one or more digits
就是说任意个数字
-111111
-11
-111
但是grep '^-[0-9]*' 这样就不行了,没有了$表示以数字结尾,那么就是一个数字后面加任意字符了
-111111
-11
-111
-11sifjisd
-11ii11
3.
when in doubt, enclose a string in single quotes,
unless it contains tildes or any expression involving a dollar sign, in which case you should use double quotes.
貌似是这样,单引号不执行,只是单纯是什么输出什么,双引号则要执行或者说是相当于没有
4.
关系运算符(< ,> ,<= ,>= ,== ,!= ,&& ,||)1为真,0为假。其余的都是0为真,1为假
5.
let表示expression是个数学运算,无须使用$(())来作进一步表明,这样的赋值方式简洁很多。
等号前后是没有空格的,在expression的表达式中也是没有空格的,如果有空格必须用引号引起来,可以是单引号,也可以是双引号
let x=1+4;let x='1 + 4';let x="1 + 4",这三个同样都是给x赋值为5。
6.
If ou want to know what indices currently have values in an array then you can use ${!array[@]}. In the last example this would return 1 45 1005.
7.
To find out the length of any element in the array, you can use ${#array[i]}.
Similarly, to find out how many values there are in the array, use * or @ as the index. So, for names=(hatter [5]=duchess alice), ${#names[5]} has the value 7, and ${#names[@]} has the value 3.
8.
如果需要限定输出的宽度,格式为%flags width.precision format-specifier,width是整数,右边对齐,如果需要左边对齐,在前面加“-”,
例如"%-20s"表示从左边开始对齐,宽度为20,如果字符串长度少于20,通过空格补齐。precision在浮点值中提供四舍五入。
例如%5.6G,长度为5,精度为6。精度是可选的。长度和精度的值可以参数中指定,例如printf "%*.*G/n" 5 6 $myvalue。长度指显示中占的字符长度,与字符长度的同义。
如果长度比实际的少,例如实际字符长度更大或者所要求的精度更大,则显示按实际长度。
9.
if [ "$var" = 42 ]if [ "$var" -eq 42 ]The first is a string comparison, the second an integer comparison.
We suggest using ((...)) for arithmetic comparisons in if statements.
- learning bash shell 学习笔记(三)
- learning bash shell 学习笔记(一)
- learning bash shell 学习笔记(二)
- Bash Shell学习笔记三
- Linux学习笔记之基本命令三(Bash Shell)
- shell(bash)学习笔记
- bash shell学习笔记
- bash shell 学习笔记
- bash shell 学习笔记
- Linux Bash Shell学习(一):开始看书《Learning the bash Shell,3rd》
- [Bash Shell] Shell学习笔记
- [Bash Shell] Shell学习笔记
- Bash Shell 学习记录三
- Learning Bash 学习bash
- Bash Shell学习笔记一
- Bash Shell学习笔记二
- Bash Shell学习笔记四
- Bash Shell学习笔记五
- 【转】地址重定位:静态重定位和动态重定位
- dede技巧:网站留言板支持用户登陆后记录用户名
- 如何分析出网站关键字
- ???
- Web中模态对话框加载后根据加载内容动态改变其大小并使其居中
- learning bash shell 学习笔记(三)
- 用VC++遍历目录
- csdn有没有手机网站啊?
- css、js两种方法实现遮罩效果
- C调用lua脚本的效率测试
- WPF从我炫系列3---内容控件的用法
- dedecms隔行不同色
- Android防火墙+流量统计代码
- [转]JQuery操作checkbox、radio等示例