• Shell脚本编程实战


    1:编写一个脚本求斐波那契数列前10项及求和

    cd /opt
    vim fibonacci.sh
    i
    echo "0+1+1+2+3+5+8+13+21+34=88"
    Shift + :
    wq
    chmod +x fibonacci.sh  # 赋予脚本执行权限
    ./fibonacci.sh  # 执行脚本
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    或者:

    a=0
    b=1
    c=0
    s=0
    for((i=0; i<10; ++i));do
    if ((i == 0));then 
    echo -n "$a"
    else echo -n "+$a"
    fi
    let s+=a
    let c=a+b
    let a=b
    let b=c
    done
    echo "=$s"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2:编写一个脚本,求一个数的逆序表示

    测试输入:15 1 256 1000;
    预期输出:
    51
    1 
    652 
    0001
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    reverseNum(){
        array_len=$# #测试集个数
        for num in $*
        do
         echo $num | rev
        done
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    3:使用Shell脚本创建文件目录

    设计一个Shell程序,在/home目录下建立一个userdata目录,在userdata目录下再建立5个目录,即user1~user5,

     mkdir -p /home/userdata
     cd /home/userdata
     mkdir -p user1 user2 user3 user4 user5
    
    • 1
    • 2
    • 3

    4: 编写一个脚本,统计每一行单词的个数

     src=/data/workspace/myshixun/src/myText.txt # 文件路径
     cd /home
     touch result.txt
     while read myline # 逐行读取文件
     do
     echo $myline | wc -w
     done < $src
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5:1~number的和

    read -p "Please input an integer number: " number
    sum=0
    for ((i=1; i<=number; i++))
    do
       sum=$[sum+i]
    done
       echo "the result of '1+2+3+...$number' is ==> $sum"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    6:显示账号名

    请写一个程序,可以将/etc/passwd的第一栏取出,而且每一栏都以一行字串“The 1 account is “root” ”来显示,那个1表示行数
    文件内容:
    rootx:0:0:root:/root:/bin/bash
    daemon:x :1:1:daemon:/usr/sbin:/usr/sbin/nologin
    bin:x :2:2:bin:/bin:/usr/sbin/nologin
    sys:x :3:3:sys:/dev:/usr/sbin/nologin
    sync:x :4:65534:sync:/bin:/bin/sync
    games:x :5:60:games:/usr/games:/usr/sbin/nologin
    man:x :6:12 : man:/var/cache/man:/usr/sbin/nologin
    lp:x :7:7:lp:/var/spool/lpd:/usr/sbin/nologin
    mail:x :8:8:mail:/var/mail:/usr/sbin/nologin
    news:x :9:9:news:/var/spool/news:/usr/sbin/nologin
    uucp:x :10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
    proxy: x:13:13:proxy:/bin:/usr/sbin/nologin
    www-data:x :33:33:www-data:/var/www:/usr/sbin/nologin
    backup:x :34:34:backup:/var/backups:/usr/sbin/nologin
    list:x :38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
    irc:x :39:39:ircd:/var/run/ircd:/usr/sbin/nologin
    gnats:x :41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
    nobody:x :65534:65534:nobody:/nonexistent:/usr/sbin/nologin
    systemd-timesync:x :100 :102:systemd Time Synchronization,:/run/systemd:/bin/false
    systemd-network:x :101:103:systemd Network Management,:/run/systemd/netif:/bin/false
    systemd-resolve:x :102:104:systemd Resolver,:/run/systemd/resolve:/bin/false
    systemd-bus-proxy:x :103:105:systemd Bus Proxy,:/run/systemd:/bin/false
    _apt:x :104:65534::/nonexistent:/bin/false
    sshd:x :105:65534::/var/run/sshd:/usr/sbin/nologin

    输出:
    The 1 account is “root”
    The 2 account is “daemon”
    The 3 account is “bin”
    The 4 account is “sys”
    The 5 account is “sync”
    The 6 account is “games”
    The 7 account is “man”
    The 8 account is “lp”
    The 9 account is “mail”
    The 10 account is “news”
    The 11 account is “uucp”
    The 12 account is “proxy”
    The 13 account is “www-data”
    The 14 account is “backup”
    The 15 account is “list”
    The 16 account is “irc”
    The 17 account is “gnats”
    The 18 account is “nobody”
    The 19 account is “systemd-timesync”
    The 20 account is “systemd-network”
    The 21 account is “systemd-resolve”
    The 22 account is “systemd-bus-proxy”
    The 23 account is “_apt”
    The 24 account is “sshd”

    str=`cat /etc/passwd | cut -d':' -f1`
    i=0
    for s in $str
    do
    i=$[i+1]
    echo "The $i account is \"$s\" "
    done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    7:Hello EduCoder

    编程要求:
    1、在/opt/目录下创建第一个shell脚本 文件命名为 test.sh;
    2、编写test.sh脚本,让其输出Hello EduCoder(通过 vim 编辑器编辑);
    3、给/opt/test.sh赋予执行权限;
    4、运行test.sh文件。  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    cd /opt
    vim test.sh
    i
    echo "Hello EduCoder"
    Esc
    Shift + :
    wq
    chmod +x ./test.sh
    ./test.sh
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    WEB开发技能树-CSS-height 100%
    如何使用生成式人工智能探索视频博客的魅力?
    OWASP Top 10漏洞解析(3)- A3:Injection 注入攻击
    【计算机网络】运输层习题(谢希仁)(3)
    Redis悠游记:缓存技术的奇迹(二)
    windows 深度学习环境部署
    二叉树的存储
    强化学习-学习笔记2 | 价值学习
    【目标检测】yolov3特征提取网络------Darknet53网络及pytorch实现
    Python绘图系统25:新增8种绘图函数
  • 原文地址:https://blog.csdn.net/qq_52331221/article/details/126570179