• 【Linux学习】——Shell编程基础


    Shell编程基础

    创建Shell脚本

    image-20201122091801966

    #!/bin/bash
    #showHllo.sh
    #To shew hello to somebody
    echo -n "Enter Your Name:"
    read NAME
    echo "Hello,$NAME!"
    

    执行脚本

    image-20201122092155257

    $ /bin/bash ShowHello.sh
    $ bash ShowHello.sh
    
    $ chmod a+x ShowHello.sh
    $ ./ShowHello.sh
    
    

    变量

    输出命令echo

    #!/bin/bash
    echo   This is a test
    echo   "This is a test"
    echo    'This is a test'
    
    
    root@pailanglee-virtual-machine:/home/pailanglee# cat 10_3.sh
    #!/bin/bash
    echo   This is a test
    echo   "This is a test"
    echo    'This is a test'
    root@pailanglee-virtual-machine:/home/pailanglee# bash 10_3.sh
    This is a test
    This is a test
    This is a test
    
    

    变量规则

    image-20201122093629991

    image-20201122093732158

    #!/bin/bash
    sal=Hello
    echo $sal
    sal="Yes Dear"
    echo $sal
    read sal
    echo " This is $sal"
    sal="Hello"
    sal=$sal",how are you."
    echo $sal
    sal=$pwd
    echo $sal
    sal=$(pwd)
    echo $sal
    
    
    
    root@pailanglee-virtual-machine:/home/pailanglee# bash bianliang.sh 
    Hello
    Yes Dear
    make
     This is make
    Hello,how are you.
    
    /home/pailanglee
    root@pailanglee-virtual-machine:/home/pailanglee# 
    
    

    image-20201122094243503

    image-20201122094625869

    #!/bin/bash
    ls *.txt>.rm_log
    rm  *.txt  
    
    root@pailanglee-virtual-machine:/home/pailanglee# touch f1.txt
    root@pailanglee-virtual-machine:/home/pailanglee# bash txtrm.sh 
    root@pailanglee-virtual-machine:/home/pailanglee# ls 
    10_3.sh     视频  音乐          cos_value.c  hello.c    main.c       test.c
    3186004050  图片  桌面          f1.sh        hello.o    makefile     ts1.lnk
    公共的      文档  a.out         haha.c       hello.pre  sin_value.c  ts2.lnk
    模板        下载  bianliang.sh  hello        hello.s    snap         txtrm.sh
    root@pailanglee-virtual-machine:/home/pailanglee# ls -a
    .           下载           .config      hello.s      .ssh
    ..          音乐           cos_value.c  .local       .sudo_as_admin_successful
    10_3.sh     桌面           f1.sh        main.c       test.c
    3186004050  a.out          .gnupg       makefile     .thunderbird
    公共的      .bash_history  haha.c       .mozilla     ts1.lnk
    模板        .bash_logout   hello        .profile     ts2.lnk
    视频        .bashrc        hello.c      .rm_log      txtrm.sh
    图片        bianliang.sh   hello.o      sin_value.c
    文档        .cache         hello.pre    snap
    
    

    image-20201122094836811

    
    #!/bin/bash
    today=$(date +%y%m%d)
    #date +%y等按照指定的格式显示
    ls /usr/bin -al>log.$today
    
    
    
    root@pailanglee-virtual-machine:/home/pailanglee# touch data.sh
    root@pailanglee-virtual-machine:/home/pailanglee# cat data.sh 
    root@pailanglee-virtual-machine:/home/pailanglee# cat > data.sh 
    #!/bin/bash
    today=$(date +%y%m%d)
    #date +%y等按照指定的格式显示
    ls /usr/bin -al>log.$today
    
    root@pailanglee-virtual-machine:/home/pailanglee# 
    root@pailanglee-virtual-machine:/home/pailanglee# bash data.sh 
    root@pailanglee-virtual-machine:/home/pailanglee# ls
    10_3.sh     图片  a.out         haha.c     hello.s      snap
    3186004050  文档  bianliang.sh  hello      log.201122   test.c
    公共的      下载  cos_value.c   hello.c    main.c       ts1.lnk
    模板        音乐  data.sh       hello.o    makefile     ts2.lnk
    视频        桌面  f1.sh         hello.pre  sin_value.c  txtrm.sh
    root@pailanglee-virtual-machine:/home/pailanglee# 
    
    

    image-20201122094902623

    #!/bin/bash
    read a
    b=$a$(pwd)
    echo $b
    
    
    
    root@pailanglee-virtual-machine:/home/pailanglee# touch reada.sh
    root@pailanglee-virtual-machine:/home/pailanglee# cat > reada.sh 
    #!/bin/bash
    read a
    b=$a$(pwd)
    echo $b
    root@pailanglee-virtual-machine:/home/pailanglee# 
    root@pailanglee-virtual-machine:/home/pailanglee# bash reada.sh 
    long long 
    long long/home/pailanglee
    
    

    image-20201122100812525

    unset命令

    image-20201122100837673

    #!/bin/sh
    foo="Hello World"
    echo $foo
    unset foo
    echo $foo
    
    
    root@pailanglee-virtual-machine:/home/pailanglee# cat > foo.sh
    #!/bin/sh
    foo="Hello World"
    echo $foo
    unset foo
    echo $foo
    root@pailanglee-virtual-machine:/home/pailanglee# 
    root@pailanglee-virtual-machine:/home/pailanglee# bash foo.sh
    Hello World
    
    
    

    引号的规则

    image-20201122101028707

    #!/bin/sh
    myvar="Hi there"
    echo $myvar
    echo "$myvar"
    echo '$myvar'
    echo $myvar
    echo Enter some text
    read myvar
    echo '$myvar' now equals $myvar
    exit 0
    
    root@pailanglee-virtual-machine:/home/pailanglee# cat > variable.sh
    #!/bin/sh
    myvar="Hi there"
    echo $myvar
    echo "$myvar"
    echo '$myvar'
    echo $myvar
    echo Enter some text
    read myvar
    echo '$myvar' now equals $myvar
    exit 0
    root@pailanglee-virtual-machine:/home/pailanglee# bash variable.sh
    Hi there
    Hi there
    $myvar
    $myvar
    Enter some text
    hi
    $myvar now equals hi
    

    环境变量

    image-20201122101322730

    image-20201122101336004

    root@pailanglee-virtual-machine:/home/pailanglee# cat huanjing.sh
    cat: huanjing.sh: 没有那个文件或目录
    root@pailanglee-virtual-machine:/home/pailanglee# cat >  huanjing.sh
    #!/bin/sh
    echo $HMOE    
    echo This is Home
    echo $PATh
    root@pailanglee-virtual-machine:/home/pailanglee# vi huanjing.sh 
    root@pailanglee-virtual-machine:/home/pailanglee# cat huanjing.sh 
    #!/bin/sh
    echo $HMOE
    echo This is Home
    echo $PATH
    
    root@pailanglee-virtual-machine:/home/pailanglee# cat>> huanjing.sh 
    echo $SHELL
    root@pailanglee-virtual-machine:/home/pailanglee# cat huanjing.sh 
    #!/bin/sh
    echo $HMOE
    echo This is Home
    echo $PATH
    
    echo $SHELL
    root@pailanglee-virtual-machine:/home/pailanglee# bash huanjing.sh 
    
    This is Home
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
    /bin/bash
    
    

    系统变量

    image-20201122101948214

    #!/bin/sh
    salutation="Hello"
    echo $salutation
    echo "The program $0 is now running"
    echo "The second parameter was $2"
    echo "The first parameter was $1"
    echo "The parameter list was $*"
    echo "The user's home directory is $HOME"
    echo "Please enter a new greeting"
    read salutation
    echo $salutation
    echo "The script is now complete"
    exit 0
    
    
    
    root@pailanglee-virtual-machine:/home/pailanglee# cat > ry_var.sh
    #!/bin/sh
    salutation="Hello"
    echo $salutation
    echo "The program $0 is now running"
    echo "The second parameter was $2"
    echo "The first parameter was $1"
    echo "The parameter list was $*"
    echo "The user's home directory is $HOME"
    echo "Please enter a new greeting"
    read salutation
    echo $salutation
    echo "The script is now complete"
    exit 0
    
    
    root@pailanglee-virtual-machine:/home/pailanglee# bash ry_var.sh 
    Hello
    The program ry_var.sh is now running
    The second parameter was 
    The first parameter was 
    The parameter list was 
    The user's home directory is /root
    Please enter a new greeting
    HI
    HI
    The script is now complete
    
    
    root@pailanglee-virtual-machine:/home/pailanglee# bash ry_var.sh a b c d
    Hello
    The program ry_var.sh is now running
    The second parameter was b
    The first parameter was a
    The parameter list was a b c d
    The user's home directory is /root
    Please enter a new greeting
    mk
    mk
    The script is now complete
    
    
  • 相关阅读:
    maven
    redis (非关系型键值对数据库) 及其 Java客户端 Jedis
    Django快速入门
    接口测试
    普通话测试-短文60篇文章,附带拼音(51-60篇)
    普通话测试-短文60篇文章,附带拼音(41-50篇)
    普通话测试-短文60篇文章,附带拼音(31-40篇)
    普通话测试-短文60篇文章,附带拼音(21-30篇)
    普通话测试-短文60篇文章,附带拼音(11-20篇)
    普通话测试-短文60篇文章,附带拼音(1-10篇)
  • 原文地址:https://www.cnblogs.com/pailang-lee/p/14353983.html
Copyright © 2020-2023  润新知