• shell基础


    echo:输出
    echo 'ls'

    替换变量:

    $(()):

    root@DESKTOP-BMKLFR3:/home/shell# a=$((32+56456))
    root@DESKTOP-BMKLFR3:/home/shell# echo $a
    56488

    $():

    root@DESKTOP-BMKLFR3:/home/shell# a=$("ls")
    root@DESKTOP-BMKLFR3:/home/shell# echo $a
    a.sh conf.tar conf1.tar test test1

    root@DESKTOP-BMKLFR3:/home/shell# a=$(ls)
    root@DESKTOP-BMKLFR3:/home/shell# echo $a
    a.sh conf1.tar test test1

    反引号:~

    root@DESKTOP-BMKLFR3:/home/shell# b=`ls`
    root@DESKTOP-BMKLFR3:/home/shell# echo $b
    a.sh conf.tar conf1.tar test test1

    alias:声明
    alias ls='ls --color=auto'当执行完这个后输入ls的时候就是其他的命令了
    echo ls
    echo $(ls)
    echo 'ls'

    变量的声明
    var=linux
    打印出当前的变量>>echo "$var" 注意是双引号,有区别

    算数运算符:
    a=$((1+2))
    echo $a

    echo 123;ls中间加分号,是按顺序去执行
    yunxin_linux@DESKTOP-BMKLFR3:~$ echo 123 ; ls ;ls
    123
    django_项目1 get-pip.py login.txt node_modules package-lock.json pro targetDirectory vender 堡垒机
    django_项目1 get-pip.py login.txt

     

    echo &&ls逻辑与,||逻辑或

    echo || ls
    yunxin_linux@DESKTOP-BMKLFR3:~$ hostname && echo "ok"
    DESKTOP-BMKLFR3
    ok

    echo 1; echo 2
    yunxin_linux@DESKTOP-BMKLFR3:~$ echo 1 ; echo 2
    1
    2

    时间处理:
    date +%Y%m%d对时间进行处理
    命令替换:
    两种方式:一个是反引号,一个是$(())
    打印出结果是echo $a
    yunxin_linux@DESKTOP-BMKLFR3:~$ date=$((1+56))
    yunxin_linux@DESKTOP-BMKLFR3:~$ echo $date
    57
    yunxin_linux@DESKTOP-BMKLFR3:~$ date=`ls`
    yunxin_linux@DESKTOP-BMKLFR3:~$ echo $date
    django_项目1 get-pip.py login.txt node_modules package-lock.json pro shell_test1.sh targetDirectory vender 堡垒机

    yunxin_linux@DESKTOP-BMKLFR3:~$ id=$(id)
    yunxin_linux@DESKTOP-BMKLFR3:~$ echo $id
    uid=1000(yunxin_linux) gid=1000(yunxin_linux) groups=1000(yunxin_linux),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),108(lxd),114(netdev),1001(docker)

    切割:
    yunxin_linux@DESKTOP-BMKLFR3:~$ cut -d: -f 7 /etc/passwd
    按照行去切割多少次


    分割出来:
    yunxin_linux@DESKTOP-BMKLFR3:~$ grep 'a' shell_test1.sh
    a=$1
    sum=$(($a+$b))

    cut -d 定义分割符,以d作为分割符
    -f 7如果是以-开头的话,后面可以加空格,也可以不加空格
    f是取字段,7取第七个字段


    例子:if判断
    max=$1
    if [$2-ge $1];then
    max=$2
    fi
    if[$3 -ge $max];then
    max=$3
    fi
    echo 'the max number is $max'

     

    if [-x /bin/nobup]##判断是否可以执行的文件
    then
    then
    fi [! -d /tmpdir]
    then
    mkdir /tmpdir
    fi
    cp /bin/nohup /tmpdir
    fi

    yunxin_linux@DESKTOP-BMKLFR3:~$ if [ 4 -eq 4 ]; then echo 4535; fi
    4535


    for循环
    yunxin_linux@DESKTOP-BMKLFR3:~$ for i in `seq 10`
    > do
    > echo $i$i
    > done
    11
    22
    33
    44
    55
    66
    77
    88
    99
    1010


    yunxin_linux@DESKTOP-BMKLFR3:~$ for i in /etc sh fgdg;do echo $i; done
    /etc
    sh
    fgdg


    yunxin_linux@DESKTOP-BMKLFR3:~$ a=`ps`
    yunxin_linux@DESKTOP-BMKLFR3:~$ echo $a
    PID TTY TIME CMD 4 tty1 00:00:01 bash 101 tty1 00:00:00 vim 134 tty1 00:00:00 vim 168 tty1 00:00:00 vim 193 tty1 00:00:00 vim 194 tty1 00:00:00 vim 211 tty1 00:00:02 man 223 tty1 00:00:00 pager 264 tty1 00:00:00 ps
    yunxin_linux@DESKTOP-BMKLFR3:~$ for i in $a
    > do
    > echo $i
    > done


    执行结果:
    ........

     

    while循环:空格,不要少写了,while:条件一直为真
    while [ 100 -lt 105 ]; do echo '开始执行' && echo ls; done

    #!bin/bash
    $ sh shell_test2.sh
    执行命令
    read,想当于是input,保存在后面的a中
    yunxin_linux@DESKTOP-BMKLFR3:~$ read -p "请输入:" a
    请输入:nihao
    yunxin_linux@DESKTOP-BMKLFR3:~$ echo $a
    nihao


    echo $RANDOM随机生成,环境变量,自带的变量

    定义函数:
    function name(){
    echo 4534534
    }
    name调用


    if:
    #!/bin/bash
    if [ $# -ne 1 ];
    then echo “参数不存在”
    exit 1
    fi
    user=$1
    if grep -1"^$user" /etc/passwd
    then
    echo "$user is not alive"
    useradd $user
    if [ $7 -eq 0 ]
    then
    echo "crerte user succful"
    else
    echo "create user failed"
    fi
    fi

    登录用户例子:
    while read -p "localhost login" user
    do

    if [[ $user =="" ]]
    then
    contine
    fi
    read -s -P "password:" passwd
    if [ $user =="root" -a X$passwd ==X"123456" ]
    then
    echo ""
    while read -p "[$user@myterminal `]]#" cmd
    do
    $cmd
    done
    else
    echo "login incrooect"
    fi
    done

     

    可以循环多个,个数打印出来
    root@DESKTOP-BMKLFR3:/home/shell# for i in $a b c d e ; do echo 1; done

     

    注意事项:
    一切皆命令,后面的都是命令
    命令替换都是$的形式来输出的,取数据和打印数据都是$符号来进行的
    注意空格

  • 相关阅读:
    475. Heaters
    69. Sqrt(x)
    83. Remove Duplicates from Sorted List Java solutions
    206. Reverse Linked List java solutions
    100. Same Tree Java Solutions
    1. Two Sum Java Solutions
    9. Palindrome Number Java Solutions
    112. Path Sum Java Solutin
    190. Reverse Bits Java Solutin
    202. Happy Number Java Solutin
  • 原文地址:https://www.cnblogs.com/yunxintryyoubest/p/10011873.html
Copyright © 2020-2023  润新知