Shell脚本编程实战

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


或者:

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


目录
相关文章
|
3天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
28 3
|
1天前
|
监控 Shell
生产环境Shell脚本Ping监控主机是否存活(多种方法)
生产环境Shell脚本Ping监控主机是否存活(多种方法)
|
7天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
30 0
|
8天前
|
监控 Shell 开发工具
Shell编程
Shell编程
|
8天前
|
Shell
Shell脚本之流程控制语句
Shell脚本之流程控制语句
|
9天前
|
JSON 运维 监控
训练shell常用脚本练习(三)
【4月更文挑战第14天】shell代码训练(三)
30 1
|
13天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
143 0
|
13天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
139 0
|
17天前
|
弹性计算 Shell Linux
ecs服务器shell常用脚本练习(六)
【4月更文挑战第4天】shell代码训练(六)
110 0
|
21天前
|
弹性计算 Shell 应用服务中间件
ecs服务器shell常用脚本练习(四)
【4月更文挑战第4天】shell代码训练(四)
96 0