简单脚本 一

查看系统中允许登陆的用户

[root@136 mnt]# vim show_loginsuer.sh
[root@136 mnt]# cat show_loginsuer.sh 
#!/bin/bash
SHELL=$(echo `grep -v nologin /etc/shells` | sed 's/ /|/g')
grep -E "$SHELL" /etc/passwd | cut -d : -f 1
[root@136 mnt]# chmod +x show_loginsuer.sh 
[root@136 mnt]# /mnt/show_loginsuer.sh 
root
student
[root@136 mnt]# useradd -s /bin/tcsh hello
[root@136 mnt]# /mnt/show_loginsuer.sh 
root
student
hello
[root@136 mnt]# 

显示ip

[root@136 mnt]# vim ip_show.sh
[root@136 mnt]# ifconfig eth0 | awk -F " " '/inet\>/{print $2}'172.25.254.136
[root@136 mnt]# awk -F "=" '/^IPAD/{print $2}' /etc/sysconfig/network-scripts/ifcfg-Ethernet_connection_1 
172.25.254.136
[root@136 mnt]# cat ip_show.sh 
#!/bin/bash
ifconfig eth0 | awk -F " " '/inet\>/{print $2}'
[root@136 mnt]# sh ip_show.sh 
172.25.254.136
[root@136 mnt]# 

统计文件行数

[root@136 mnt]# awk 'BEGIN{N=0}{N++}END{print N}' passwd 
28
[root@136 mnt]# vim tongjihang.sh
[root@136 mnt]# cat tongjihang.sh 
#!/bin/bash
awk 'BEGIN{N=0}{N++}END{print N}' passwd 
[root@136 mnt]# sh tongjihang.sh 
28

安装 httpd,并修改端口为所跟port

[root@136 ~]# vim install_apache.sh
[root@136 ~]# cat install_apache.sh
#!/bin/bash
[ "$#" -eq "1" ]||{
    echo "Please input port after script!!"
    exit 1
}
yum install httpd -y &> /dev/null
sed "/^Listen/cListen $1" -i /etc/httpd/conf/httpd.conf
systemctl restart httpd
echo -e "\033[32mThe Listen is changed!!\033[0m"
netstat -antulpe | grep http
[root@136 ~]# sh install_apache.sh 66
The Listen is changed!!
tcp6       0      0 :::66                   :::*                    LISTEN      0          70735      2584/httpd 

实现在执行脚本后跟两个参数,userfile、passfile,创建userfile中的用户,并添加passfile中的密码给用户

[root@136 ~]# vim userfile
[root@136 ~]# cat userfile 
user1
user2
user3
[root@136 ~]# vim passfile
[root@136 ~]# cat userfile 
user1
user2
user3
[root@136 ~]# vim create_user.sh
[root@136 ~]# sh create_user.sh userfile passfile 
user1
123
user2
123
user3
123
[root@136 ~]# cat create_user.sh 
#!/bin/bash
MAX_LINE=`wc -l $1 | cut -d " " -f 1`
for LINE_NUM in `seq 1 $MAX_LINE`
do
    sed -n "${LINE_NUM}p" $1
    sed -n "${LINE_NUM}p" $2
done
[root@136 ~]# vim create_user.sh
[root@136 ~]# cat create_user.sh 
#!/bin/bash
MAX_LINE=`wc -l $1 | cut -d " " -f 1`
for LINE_NUM in `seq 1 $MAX_LINE`
do
    USERNAME=`sed -n "${LINE_NUM}p" $1`
    PASSWORD=`sed -n "${LINE_NUM}p" $2`
    useradd $USERNAME
    echo $PASSWORD | passwd --stdin $USERNAME
done
[root@136 ~]# sh create_user.sh userfile passfile 
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.

若没有跟两个文件参数,将报错

[root@136 ~]# vim create_user.sh
[root@136 ~]# cat create_user.sh 
#!/bin/bash
[ "$#" -ne "2" ]&&{
    echo -e "\033[31mERROR:please input userfile and passwordfile\033[0m"
    exit 1
}
MAX_LINE=`awk 'BEGIN{N=0}{N++}END{print N}' $1`
for LINEMAX in `seq 1 $MAX_LINE`
do
         USERNAME=$(sed -n "${LINEMAX}P" $1)
         PASSWORD=$(sed -n "${LINEMAX}P" $2)
         useradd $USERNAME
         echo $PASSWORD | passwd --stdin $USERNAME
done
[root@136 ~]# sh create_user.sh 
ERROR:please input userfile and passwordfile
[root@136 ~]# sh create_user.sh userfile 
ERROR:please input userfile and passwordfile
[root@136 ~]# sh create_user.sh userfile passfile 
useradd: user 'user1' already exists
Changing password for user user1.
passwd: all authentication tokens updated successfully.
useradd: user 'user2' already exists
Changing password for user user2.
passwd: all authentication tokens updated successfully.
useradd: user 'user3' already exists
Changing password for user user3.
passwd: all authentication tokens updated successfully.
[root@136 ~]# 

修改为交互

[root@136 ~]# 
[root@136 ~]# vim create_user.sh
[root@136 ~]# cat create_user.sh 
#!/bin/bash
read -p "Please input userfile:" USER
[ -e "$USER" ]||{
    echo -e "\033[31mERROR: $USER is not exist!!\033[0m"
    exit 1
}
read -p "Please input passwordfile:" PASSWORDFILE
[ -e "$PASSWORDFILE" ]||{
    echo "\033[31mERROR: $PASSWORDFILE is not exist!!\033[0m"
    exit 1
}
MAX_LINE=`awk 'BEGIN{N=0}{N++}END{print N}' $USER`
for LINEMAX in `seq 1 $MAX_LINE`
do
         USERNAME=$(sed -n "${LINEMAX}P" $USER)
         PASSWORD=$(sed -n "${LINEMAX}P" $PASSWORDFILE)
         useradd $USERNAME
         echo $PASSWORD | passwd --stdin $USERNAME
done

[root@136 ~]# sh create_user.sh 
Please input userfile:userfile
Please input passwordfile:passfile
useradd: user 'user1' already exists
Changing password for user user1.
passwd: all authentication tokens updated successfully.
useradd: user 'user2' already exists
Changing password for user user2.
passwd: all authentication tokens updated successfully.
useradd: user 'user3' already exists
Changing password for user user3.
passwd: all authentication tokens updated successfully.

脚本中的函数是把一个复杂的语句块定义成一个字符串的方法

[root@136 ~]# vim ip.sh
[root@136 ~]# cat ip.sh 
#!/bin/bash
PING()
{
    read -p "Please input a idaddress: " IP
    ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down
    PING
}
PING
[root@136 ~]# sh ip.sh 
Please input a idaddress: 172.25.254.63
172.25.254.63 is up
Please input a idaddress: 172.25.254.11
172.25.254.11 is down
Please input a idaddress: ^C
[root@136 ~]# 

增加输入exit退出

[root@136 ~]# vim ip.sh
[root@136 ~]# cat ip.sh 
#!/bin/bash
PING()
{
    read -p "Please input a idaddress: " IP
    [ "$IP" = exit ]&&{
        echo bye
        exit 0
    }
    ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down
    PING
}
PING
[root@136 ~]# sh ip.sh 
Please input a idaddress: 172.25.254.63
172.25.254.63 is up
Please input a idaddress: exit
bye
[root@136 ~]# 
[root@136 ~]# vim file_check.sh
[root@136 ~]# cat file_check.sh 
#!/bin/bash
FILE_CHECK()
{
    [ "$1" "$2" ]&&{
        echo $2 is $3
        exit 0
    }
}
[ -e "$1" ]||{
    echo $1 is not exist!!
    echo 0
}
FILE_CHECK -L $1 link
FILE_CHECK -f $1 file
[root@136 ~]# sh file_check.sh /etc/passwd
/etc/passwd is file
[root@136 ~]# sh file_check.sh /etc/system-release
/etc/system-release is link
[root@136 ~]# vim useraction_check.sh
[root@136 ~]# cat useraction_check.sh 
#!/bin/bash
USERADD(){
    [ "$1" = add ]&&{
    read -p "plesae input addusername: " USERA
        read -p "plesae input addpassword: " -s PASSWORD
        echo ""
    useradd  $USERA
        echo $PASSWORD | passwd --stdin $USERA
        }
}
USERDEL(){
    [ "$1" = del ]&&{
        read -p "plesae input username: " USERNAME
        userdel -r $USERNAME
    }
}
USERACTION(){
    read -p "please input action: " ACTION
    [ "$ACTION" = exit ]&&{
        echo "bye"
        exit 0
    }
    USERADD $ACTION
    USERDEL $ACTION
    USERACTION
}
USERACTION
[root@136 ~]# sh useraction_check.sh 
please input action: add
plesae input addusername: haha
plesae input addpassword: 
Changing password for user haha.
passwd: all authentication tokens updated successfully.
please input action: del
plesae input username: haha
please input action: 123
please input action: exit
bye

猜你喜欢

转载自blog.csdn.net/awoyaoc/article/details/80836918