shell脚本_Hello world

linux环境:

     helloworld:

         1、新建一个文件helloWord.sh,shell脚本文件格式一般为.sh

         2、文件中写入

                 #! /bin/bash                     脚本的解释程序路径

                 echo "hello wolrd"           终端输出hello world,echo是在终端打印

         3、将文件helloWord.sh权限修改为可执行脚本,chmod u+x helloWord.sh

         4、执行脚本,./helloWord.sh 

         5、控制台输出

    一个简单的shell脚本写完。

    

     常用语法:

       1、变量:

              var log = "monday" 

              echo $log                           变量使用$引用

                输出monday

         2、引号中变量使用

             var log = "monday"

             echo "today is ${log}out"

                输出:today is mondayout

         3、/转义字符

              echo /$

                 输出$

         4、引号的使用(双引号,单引号,到引号)

                var log = monday

                echo "today is $log"

                echo 'today is $log'

                echo "today is `date`"        

            输出分别是: today is monday

                                today is $log

                                today is 2016年4月21号    

            可以看出三个引号的区别为

            引号:阻止编译器对引号内容进行解释,除了$,",`   还保持其特殊含义

            单引号:阻止编译器对引号中所有内容解释,原封不动的输出

            到引号:倒引号中是可执行 命令 

        5、条件语句

            if语句:

                if [ "$1" = "hello" ] ; then
                        echo "hello!"
                elif [ "$1" = ""  ]; then
                        echo " no parameter"
                else
                        echo "only hello "
                fi

            case语句:          

            case $num in

                条件1) echo "January";;             双分号结束

                条件2) echo "Feburary";;

                条件5) echo "may"                    每个case可以有多条命令       

                       echo "sdfd"

                       echo "sdf";;                        但最后一条命令一定是双分号结束

                *) echo "not correct input";;      *)是其他值、default的意思

                esac    

    6、循环语句

                sum = 0;                                        

                num = 1;

                while test $number -le 100                       ( le·:<=    lt:<    ge:>=  gt:>)

                do

                    sum = $[ $sum + $num ]

                    let num = $num +1                                   let是整数算数运算的命令 

  1.                 done

                echo "the sum is $sum"            

      7、sort排序命令

            sort 文件名            将文件根据行排序

            sort -r 文件名        将文件根据行反序排序

            sort -k 2 文件名    根据文件的第二列进行排序

            sort 文件名 |uniq           将文件根据行排序并且去除重复

       8、tr命令

            tr "BHS" "x"<文件名          将文件中的B、H、S都替换为x

            tr --delete "ei" <文件名      将文件中的e、i删除

       9wc命令

             wc 文件                  输出: 文件行数  单词书  字节数  文件名

             wc -c  文件名        只显示文件自己数

             wc -l     文件名       只文件行数

             wc -w 文件名          只显示文件单词数

       10substr命令

                substr  操作字符  提取字符起始位置  提取字符长度

                substr "hello world" 1 5 结果 hello

        11、seq

                seq 8             产生1-8的整数数列

                seq -1 3         产生-1到3的数列

                seq 9 -3   0    每一个比上一个数小3,最小为0

         12、printenv  查看当前系统所有已经定义的变量

原文地址:https://www.cnblogs.com/guoliangxie/p/5416762.html