文章目录
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例的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/shellbc/1554.html