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.

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 二审判决后不服的怎么办 对树木对生物花草过敏怎么办 高一英语成绩差怎么办 文章被同时录用该怎么办 来不及开ei检索证明怎么办 淘宝生产许可编号一定要填怎么办 官网下单被砍单怎么办美卡美私 大学网络课程挂了怎么办 如果二审超过6个月还不判怎么办? sci的proof时间超了怎么办 合肥电大考试没过怎么办 社保账号密码忘记了怎么办 住房公积金账号密码忘记了怎么办 燃气费单子丢了怎么办 商标初审公告期内被异议怎么办 手被山药痒了怎么办 9个月宝宝不吃奶粉怎么办 八个月宝宝拉粑粑费劲怎么办? 两个月小孩不吃奶粉怎么办 两个月的小孩不吃奶粉怎么办 两个多月宝宝不吃奶怎么办 三个多月宝宝不爱吃奶怎么办 4个月宝宝不吃奶怎么办 5个月宝宝不爱吃奶怎么办 九个月宝宝一直流鼻涕怎么办 九个月宝宝一直咳嗽怎么办 宝宝3岁不爱喝水怎么办 1岁宝宝不肯喝水怎么办 三个月宝宝体检说严重缺钙怎么办 1岁半宝宝不吃药怎么办 1岁宝宝抗拒吃药怎么办 六个月宝宝不爱吃辅食怎么办 宝宝九个月了不爱吃辅食怎么办 八个月宝宝不喜欢吃辅食怎么办 小孩米粉吃多了怎么办 宝宝四个月了奶水不足怎么办 4个月奶水不足怎么办 孩子不吃奶粉母乳又不够怎么办 宝宝吃母乳上火了怎么办 5个月宝宝厌奶期怎么办 九个月宝宝不吃奶粉怎么办