设为首页 收藏本站
查看: 1308|回复: 0

learning bash shell 学习笔记(一)

[复制链接]

尚未签到

发表于 2015-10-26 10:41:44 | 显示全部楼层 |阅读模式
  1.通配符也可以用于目录
  
   For example, if you wanted to list all of the files in the directories /usr and /usr2, you could type ls /usr*. If you were only interested in the files beginning with the letters b and e in these directories, you could type ls /usr*/[be]* to list them.
  
  2.花括号扩展嵌套
  
  It is also possible to nest the braces, as in b{ar{d,n,k},ed}s. This will result in the expansion bards, barns, barks, and beds. 此外花括号扩展要求至少有一个逗号 b{o}lt remains as b{o}lt.
  3.
  
  快捷键: CTRL-A and CTRL-E for beginning and end of line
  通配符扩展又叫globbing: wildcard expansion is sometimes called globbing
  变量命名时:
  varname=value
  
  here must be no space on either side of the equal sign, and if the value is more than one word, it must be surrounded by quotes
  
  echo 当用双引号时,当成一个整体的字符串;当用单引号时,是splits the string into words 然后打印,所有后面的空格就被截断了
  
  4.局部变量,全局变量,变量的作用域
  命令行的几个参数变量时局部的,除了$0一直表示脚本的名字,其余$1、$2....都是局部的,但是其他的非命令行参数的变量却是全局的,跟C语言什么的还有很大差别

Typically, several shell functions are defined within a single shell script. Therefore each function will need to handle its own arguments,
which in turn means that each function needs to keep track of positional parameters separately.
Sure enough, each function has its own copies of these variables (even though functions don't run in their own subshells, as scripts do);
we say that such variables are local to the function.
However, other variables defined within functions are not local (they areglobal), meaning that their values are known throughout the entire shell script.
For example, assume that you have a shell script called ascript that contains this:

function afunc
{
echo in function: $0 $1 $2
var1="in function"
echo var1: $var1
}

var1="outside function"
echo var1: $var1
echo $0: $1 $2
afunc funcarg1 funcarg2
echo var1: $var1
echo $0: $1 $2


  
If you invoke this script by typing ascript arg1 arg2, you will see this output:

var1: outside function
ascript: arg1 arg2
in function: ascript funcarg1 funcarg2
var1: in function
var1: in function
ascript: arg1 arg2

  
In other words, the function afunc changes the value of the variable var1 from "outside function" to "in function," and that change is known outside the function, while $1 and $2 have different values in the function and the main script. Notice that $0 doesn't change because the function executes in the environment of the shell script and $0 takes the name of the script.

当用local声明一个变量时,则是局部的,如果处于函数中与外部全局变量重名,则覆盖外部全局变量

function afunc
{
local var1
echo in function: $0 $1 $2

var1="in function"
echo var1: $var1
}


Now the result of running ascript arg1 arg2 is:

var1: outside function
ascript: arg1 arg2
in function: ascript funcarg1 funcarg2
var1: in function
var1: outside function
ascript: arg1 arg2

Figure 4-3 shows the scope of each variable in our new script. Note that afunc now has its own, local copy of var1, although the original var1 would still be used by any other functions that ascript invokes.
  
  

Figure 4-3. Functions can have local variables

  
                                                 DSC0000.gif


5.

sort -nr $1 | head -${2:-10}

Here is how this works: the sort program sorts the data in the file whose name is given as the first argument ($1). The -n option tells sort to interpret the first word on each line as a number (instead of as a character string); the -r tells it to reverse the comparisons, so as to sort in descending order.
The output of sort is piped into the head utility, which, when given the argument -N, prints the first N lines of its input on the standard output. The expression -${2:-10} evaluates to a dash (-) followed by the second argument if it is given, or to -10 if it's not; notice that the variable in this expression is 2, which is the second positional parameter.
Assume the script we want to write is called highest. Then if the user types highest myfile, the line that actually runs is:

sort -nr myfile | head -10

Or if the user types highest myfile 22, the line that runs is:

sort -nr myfile | head -22
Expression                   Result
${path##/*/}                      long.file.name
${path#/*/}              cam/book/long.file.name
$path              /home/cam/book/long.file.name
${path%.*}         /home/cam/book/long.file
${path%%.*}        /home/cam/book/long

6.Command Substitution
To find out detailed information about a command if you don't know where its file resides, type ls -l $(type -path -all command-name).
The -all option forces type to do a pathname look-up and -path causes it to ignore keywords, built-ins, etc.

When in doubt, use single quotes, unless the string contains variables or command substitutions, in which case use double quotes
什么意思呢?
cut -fN -dC filename, use this instead: awk -FC '{print $N}' filename
mail $(who | cut -d' ' -f1)   $后用括号扩起命令,可以是取值,类似重定向
   
版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-130883-1-1.html 上篇帖子: shell之变量 下篇帖子: Linux Shell的通配符与正则表达式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表