美文网首页
Shell (三)

Shell (三)

作者: StarShift | 来源:发表于2016-11-18 13:21 被阅读37次

shell中的特殊字符

- # 注释字符
- ; 行分割字符
- ;; case 终结字符
- . source 命令
- :空命令
注释

以 # 开头的行是一行注释。

#this is a  comment

#号可以在本行的空白或者tab后面

            #this is also a comment

注释也可以嵌入在管道中:

initial=( `cat "$startfile" | sed -e '/#/d' | tr -d '\n' |\
# Delete lines containing '#' comment character.
           sed -e 's/\./\. /g' -e 's/_/_ /g'` )
# Excerpted from life.sh script

注意:有些shell命令是在一行中的,对于这种命令,shell注释并没有提供注释段的完结表示符。因此后面的内容都会被注释掉,如果想继续写命令,需要另起一行。

对于下面的情况:

#!/bin/bash
echo "The # here does not begin a comment."
echo 'The # here does not begin a comment.'
echo The \# here does not begin a comment.
echo The # here begins a comment.

echo ${PATH#*:}       # Parameter substitution, not a comment.
echo $(( 2#101011 ))  # Base conversion, not a comment.
  • 在echo后面被引号引起来的,或者转义之后的不是注释。
  • 参数替换,也不是注释
  • 进制转换,也不是注释

关于case

#!/bin/bash
variable=$1
case "$variable" in
abc) echo "\$variable = abc";;
xyz) echo "\$variable = xyz";;
esac

空命令

等价于Nop, 什么都不做。运行之后,它的返回值为0,也就是shell的true。

;
echo $?   #0

或者在一个死循环中:

while :
do
   operation-1
   operation-2
   ...
   operation-n
done

# Same as:
#    while true
#    do
#      ...
#    done

在if 中占用空的分支,在shell中,每个分支必须要有动作。

if condition
then :   # Do nothing and branch ahead
else     # Or else ...
   take-some-action
fi

另外可以用作命令占位符,例如如下的场景。

需要将当前用户名通过反引号赋值给username 。

: ${username=`whoami`}
# ${username=`whoami`}   Gives an error without the leading :
#                        unless "username" is a command or builtin...
#!/bin/bash
: ${username=`whoami`}
echo $username

阻塞,防止产生新进程。

: >data

相关文章

  • shell 案例

    Shell编程一 Shell防范ARP攻击 Shell编程二 Shell防范DDos攻击 Shell编程三 ...

  • Xamarin.Forms 第28局:Shell

    目录 - Xamarin.Forms 前言 本文介绍Shell:一、Shell简介二、Shell构建三、Shell...

  • 目录

    shell 一、 初识shell二、 shell变量三、 正则表达式四、 认识脚本和shell判断表达式五、she...

  • Day56-Shell编程入门

    第三个阶段:Shell\Jenkins\Git 1.Shell课程大纲 7天 动手!!! 1.Shell ...

  • shell编程二

    目录 一、shell中的函数二、shell中的数组三、shell告警系统 一、shell中的函数 函数就是把一段代...

  • MAC

    Mac开发必备工具(三)—— Fish shell 开箱即用的Shell,非常适合Shell初学者,没有繁琐的配置...

  • Shell基础 -Linux从入门到精通第九天(非原创)

    文章大纲 一、关于shell二、shell进阶(重点)三、学习资料下载四、参考文章 一、关于shell 1. 什么...

  • 2019-01-05shell编程之变量的定义

    一、shell能做什么? 二、shell语言的执行方式 三、shell变量的定义 1,自定义变量: 特性: 赋值 ...

  • shell(三)

    Shell 传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n。n 代表一...

  • Shell (三)

    shell中的特殊字符 注释 以 # 开头的行是一行注释。 #号可以在本行的空白或者tab后面 注释也可以嵌入在管...

网友评论

      本文标题:Shell (三)

      本文链接:https://www.haomeiwen.com/subject/igvwuttx.html