日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

脚本的练习示例一

發布時間:2025/3/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 脚本的练习示例一 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 顯示系統中能登陸系統的用戶

[root@desktop27 mnt]# vim show_loginuser.sh [root@desktop27 mnt]# cat show_loginuser.sh #!/bin/bash SHELL=$(echo `grep -v nologin /etc/shells` | sed 's/ /|/g') grep -E "$SHELL" /etc/passwd | cut -d : -f 1 [root@desktop27 mnt]# chmod +x show_loginuser.sh [root@desktop27 mnt]# /mnt/show_loginuser.sh root student [root@desktop27 mnt]# useradd -s /bin/tcsh tutu [root@desktop27 mnt]# su - tutu [tutu@desktop27 ~]$ logout [root@desktop27 mnt]# /mnt/show_loginuser.sh root student tutu [root@desktop27 mnt]#
  • 顯示ip

[root@desktop27 mnt]# ifconfig eth0 | awk -F " " '/inet\>/{print $2}' 172.25.254.127 [root@desktop27 mnt]# awk -F "=" '/^IPAD/{print $2}' /etc/sysconfig/network-scripts/ifcfg-eth0 172.25.254.127 [root@desktop27 mnt]# vim file.sh [root@desktop27 mnt]# cat file.sh #!/bin/bash ifconfig eth0 | awk -F " " '/inet\>/{print $2}' [root@desktop27 mnt]# sh file.sh 172.25.254.127 [root@desktop27 mnt]#
  • 統計行數

[root@desktop27 mnt]# awk 'BEGIN{N=0}{N++}END{print N}' passwd 12 [root@desktop27 mnt]# vim test.sh [root@desktop27 mnt]# cat test.sh #!/bin/bash awk 'BEGIN{N=0}{N++}END{print N}' passwd [root@desktop27 mnt]# sh test.sh 12 [root@desktop27 mnt]#
  • 執行腳本:安裝 httpd,并修改端口為所跟port

[root@desktop27 mnt]# vim install_apache.sh [root@desktop27 mnt]# 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@desktop27 mnt]# sh install_apache.sh 80 The Listen is changed!! tcp6 0 0 :::443 :::* LISTEN 0 69833 4800/httpd tcp6 0 0 :::80 :::* LISTEN 0 69815 4800/httpd [root@desktop27 mnt]#
  • 實現在執行腳本后跟兩個參數,userfile、passfile,創建userfile中的用戶,并添加passfile中的密碼給用戶

[root@desktop27 mnt]# vim userfile [root@desktop27 mnt]# vim passfile [root@desktop27 mnt]# cat userfile user1 user2 user3 [root@desktop27 mnt]# cat passfile user1123 user2123 user3123 [root@desktop27 mnt]# vim create_user.sh [root@desktop27 mnt]# cat create_user.sh #!/bin/bash MAX_LINE=`wc -l $1 | cut -d " " -f 1` for LINE_NUM in `seq 1 $MAX_LINE` dosed -n "${LINE_NUM}p" $1sed -n "${LINE_NUM}p" $2 done [root@desktop27 mnt]# sh create_user.sh userfile passfile user1 user1123 user2 user2123 user3 user3123 [root@desktop27 mnt]# vim create_user.sh [root@desktop27 mnt]# cat create_user.sh #!/bin/bash MAX_LINE=`wc -l $1 | cut -d " " -f 1` for LINE_NUM in `seq 1 $MAX_LINE` doUSERNAME=`sed -n "${LINE_NUM}p" $1`PASSWORD=`sed -n "${LINE_NUM}p" $2`useradd $USERNAMEecho $PASSWORD | passwd --stdin $USERNAME done [root@desktop27 mnt]# 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@desktop27 mnt]#
  • 前一腳本的改進:若沒有跟兩個文件參數,將報錯

[root@desktop27 mnt]# vim create_user.sh [root@desktop27 mnt]# 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` doUSERNAME=$(sed -n "${LINEMAX}P" $1)PASSWORD=$(sed -n "${LINEMAX}P" $2)useradd $USERNAMEecho $PASSWORD | passwd --stdin $USERNAME done [root@desktop27 mnt]# sh create_user.sh ERROR:please input userfile and passwordfile [root@desktop27 mnt]# sh create_user.sh userfile ERROR:please input userfile and passwordfile [root@desktop27 mnt]# 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@desktop27 mnt]#
  • 再次改進:實現交互

[root@desktop27 mnt]# vim create_user.sh [root@desktop27 mnt]# 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` doUSERNAME=$(sed -n "${LINEMAX}P" $USER)PASSWORD=$(sed -n "${LINEMAX}P" $PASSWORDFILE)useradd $USERNAMEecho $PASSWORD | passwd --stdin $USERNAME done [root@desktop27 mnt]# sh create_user.sh Please input userfile:userfile Please input passwordfile: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@desktop27 mnt]#
  • 腳本中的函數


    腳本中的函數是把一個復雜的語句塊定義成一個字符串的方法
    例如:
  • 例一

[root@desktop27 mnt]# vim file.sh [root@desktop27 mnt]# cat file.sh #!/bin/bash PING() {read -p "Please input a idaddress: " IPping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is downPING } PING [root@desktop27 mnt]# sh file.sh Please input a idaddress: 172.25.254.127 172.25.254.127 is up Please input a idaddress: 172.25.254.227 172.25.254.227 is down Please input a idaddress: 172.25.254.50 172.25.254.50 is up Please input a idaddress: ^C [root@desktop27 mnt]# ##因為腳本中沒有退出設定,所以 crtl+c 退出
  • 例一改進

[root@desktop27 mnt]# vim file.sh [root@desktop27 mnt]# cat file.sh #!/bin/bash PING() {read -p "Please input a idaddress: " IP[ "$IP" = exit ]&&{echo byeexit 0}ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is downPING } PING [root@desktop27 mnt]# sh file.sh Please input a idaddress: 172.25.254.127 172.25.254.127 is up Please input a idaddress: exit bye [root@desktop27 mnt]#
  • 例二

[root@desktop27 mnt]# vim file_check.sh [root@desktop27 mnt]# cat file_check.sh #!/bin/bash FILE_CHECK() {[ "$1" "$2" ]&&{echo $2 is $3exit 0} } [ -e "$1" ]||{echo $1 is not exist!!echo 0 } FILE_CHECK -L $1 link FILE_CHECK -f $1 file [root@desktop27 mnt]# sh file_check.sh /etc/system-release /etc/system-release is link [root@desktop27 mnt]# sh file_check.sh /etc/passwd /etc/passwd is file [root@desktop27 mnt]#
  • 例三

[root@desktop27 mnt]# vim useraction_check.sh [root@desktop27 mnt]# cat useraction_check.sh #!/bin/bash USERADD(){[ "$1" = add ]&&{read -p "plesae input addusername: " USERAread -p "plesae input addpassword: " -s PASSWORDecho ""useradd $USERAecho $PASSWORD | passwd --stdin $USERA} } USERDEL(){[ "$1" = del ]&&{read -p "plesae input username: " USERNAMEuserdel -r $USERNAME} } USERACTION(){read -p "please input action: " ACTION[ "$ACTION" = exit ]&&{echo "bye"exit 0}USERADD $ACTIONUSERDEL $ACTIONUSERACTION } USERACTION [root@desktop27 mnt]# sh useraction_check.sh please input action: add plesae input addusername: tutu plesae input addpassword: Changing password for user tutu. passwd: all authentication tokens updated successfully. please input action: del plesae input username: tutu please input action: add plesae input addusername: user1 plesae input addpassword: Changing password for user user1. passwd: all authentication tokens updated successfully. please input action: del plesae input username: user1 please input action: exit bye [root@desktop27 mnt]#

總結

以上是生活随笔為你收集整理的脚本的练习示例一的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。