当前位置:网站首页 > Shell脚本编程 > 正文

shell脚本应用实战_shell脚本编程100例

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 # 执行脚本 

或者:

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" 

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

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

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

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

 mkdir -p /home/userdata cd /home/userdata mkdir -p user1 user2 user3 user4 user5 

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

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

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" 

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 

7:Hello EduCoder !

编程要求: 1、在/opt/目录下创建第一个shell脚本 文件命名为 test.sh; 2、编写test.sh脚本,让其输出Hello EduCoder(通过 vim 编辑器编辑); 3、给/opt/test.sh赋予执行权限; 4、运行test.sh文件。 
cd /opt vim test.sh i echo "Hello EduCoder" Esc Shift + : wq chmod +x ./test.sh ./test.sh 
到此这篇shell脚本应用实战_shell脚本编程100例的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • shell脚本_shell脚本实例100例2024-11-02 23:50:10
  • shell编程简单实例_shell在线编程2024-11-02 23:50:10
  • 优秀的shell脚本_shell脚本教程2024-11-02 23:50:10
  • Linux学习之Linux命令行与shell脚本编程大全2024-11-02 23:50:10
  • shell脚本语言入门_Linux怎么学2024-11-02 23:50:10
  • shell脚本语言(超全超详细)_shell脚本语言入门2024-11-02 23:50:10
  • Linux命令行与shell脚本编程大全(shell脚本编程基础部分)_linux shell命令行及脚本编程实例详解2024-11-02 23:50:10
  • 在centos下通过shell脚本拉取git代码并部署2024-11-02 23:50:10
  • 通过shell脚本运行jar包,报错nohup: failed to run command ‘java’: No such file or directory2024-11-02 23:50:10
  • shell编程简单实例(shell编程总结)2024-11-02 23:50:10
  • 全屏图片