Shell的基础脚本练习合集
程序员文章站
2022-03-30 10:17:10
练习1:创建一个脚本ip_show.sh,使得执行此脚本可显示当前ip和主机名称#!/bin/bashifconfig virbr0 &> /dev/null && { ifconfig virbr0 | awk '/inet\>/{print$2}'} || { echo virbr0 is not found}hostname练习2:创建一个脚本host_messages.sh,使得执行此脚本可显示当....
目录
练习1 显示ip和主机名称
创建一个脚本ip_show.sh,使得执行此脚本可显示当前ip和主机名称
#!/bin/bash
ifconfig virbr0 &> /dev/null && {
ifconfig virbr0 | awk '/inet\>/{print$2}'
} || {
echo virbr0 is not found
}
hostname
练习2 显示主机名称+ip+登录用户
创建一个脚本host_messages.sh,使得执行此脚本可显示当前主机名称、ip、和登陆主机的用户
#!/bin/bash
echo "hostname: `hostname`"
echo "ipaddress: `ifconfig virbr0 | awk '/inet\>/{print$2}' `"
echo "username: `whoami`"
练习3 清空日志
创建一个脚本clear_log.sh,使得执行此脚本可清空日志
#!/bin/bash
[ "$USER" = "root" ] && {
find /var/log -maxdepth 1 -type f -exec cp /dev/null {} \;
}||{
echo $0 is must be used by root
}
练习4 用户建立
创建一个脚本create_user.sh,执行此脚本可使脚本后文件username中的所有用户被建立
#!/bin/bash
[ -z "$*" ] && {
echo "ERROR: please input userfile!"
exit
}||{
[ -f "$*"]||{
echo "ERROR : $* is not a file"
exit
}
}
for USERNAME in `cat $*`
do
useradd USERNAME &> /dev/null ||{
echo $USERNAME is exist
}
done
练习5 用户控制(建立+删除)
创建一个脚本userctrl.sh,使得执行userctrl.sh delete user1 会删除user1;执行userctrl.sh add user1 会创建user1
#!/bin/bash
[ "$#" -lt "2" ] && {
echo "please input action and username following $0"
exit
}
[ "$1" = "delete" -o "$1" = "add" ] ||{
echo "Usage: $0 delete|add username"
exit
}
[ "$1" = "add" ]&& {
id $2 &> /dev/null && {
echo $2 is exist
exit
}||{
useradd $2
echo $2 is created
}
}||{
id $2 &> /dev/null &&{
userdel -r $2
}||{
echo "$2 is not exist"
exit
}
}
本文地址:https://blog.csdn.net/weixin_46833747/article/details/107160543
上一篇: android studio 新项目导入时怎么设置
下一篇: postgreSql DDL 分区表