1.更改字體或背景的顏色
echo -e “\033[背景顏色代碼;字體顏色代碼m內容\033[0m”
字背景顏色:(40黑)(41深紅)(42綠)(43黃)(44藍)(45紫)(46深綠)(47白)
字顏色:(30黑)(31紅)(32綠)(33黃)(34藍)(35紫)(36深綠)(37白)
1.顯示字體的顏色為紅色=
[root@shell_example mnt]# echo -e "\033[31mhello\033[0m"
2.顯示背景顏色為黑色,字體顏色為紅色
[root@shell_example mnt]# echo -e "\033[40;31mhello\033[0m"
2.更改http服務的端口
編寫一個shell腳本:
[root@shell_example mnt]# vim apache_port.sh
腳本內容如下:
#!/bin/bash
[ -z "$1" ] && {
echo -e "\033[31mError: Please input port number Apache Server\033[0m"
exit
}
sed "/^Listen/cListen $1" -i /etc/httpd/conf/httpd.conf
systemctl restart httpd &>/dev/null
port=`netstat -antlupe | grep httpd | awk '{print $4}' | cut -d : -f 4`
[ "$1" -eq "$port" ] && {
echo -e "\033[32mPort has change to $1 \033[0m"
}||{
echo -e "\033[31mError:Change port failed\033[0m"
}
運行腳本,進行測試:
不輸入任何內容時:
[root@shell_example mnt]# sh apache_port.sh
輸入非法內容時:
[root@shell_example mnt]# sh apache_port.sh hh
輸入其他端口號時:
[root@shell_example mnt]# sh apache_port.sh 8080
檢測端口號是否與設置的相同
[root@shell_example mnt]# netstat -antlp | grep httpd
[root@shell_example mnt]# netstat -antlp | grep httpd | awk '{print $4}'
[root@shell_example mnt]# netstat -antlp | grep httpd | awk '{print $4}' | cut -d : -f 4
3.編寫一個shell腳本使其使用for循環,從1輸出到10
(1)編輯一個shell腳本
[root@shell_example mnt]# vim file.sh
腳本內容如下:
#!/bin/bash
for i in $(seq 1 10)
do
echo $i
done
運行腳本:
[root@shell_example mnt]# sh file.sh
4.建立一個shell腳本,使其可以讀取user_file(三個已經給了的用戶)和pass_file(三個已經給了的密碼)中的內容,沒有創建的用戶進行創建,密碼進行修改,如果用戶已存在,則顯示用戶已經存在
(1)建立一個user_file文件,寫入三個用戶
[root@shell_example mnt]# vim user_file
[root@shell_example mnt]# cat user_file
(2)建立一個pass_file文件,寫入三個密碼
[root@shell_example mnt]# vim pass_file
[root@shell_example mnt]# cat pass_file
(3)編寫一個shell腳本
[root@shell_example mnt]# vim user_create.sh
腳本內容如下:
#!/bin/bash
Max_Line=`sed -n '$=' $1`
for i in $(seq 1 $Max_Line)
do
USERNAME=`sed -n "${i}p" $1`
PASSWORD=`sed -n "${i}p" $2`
id $USERNAME &> /dev/null && {
echo "$USERNAME is exist"
}||{
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME &>/dev/null && echo $USERNAME CREATE
}
done
(4)腳本運行結果如下:
[root@shell_example mnt]# sh user_create.sh user_file pass_file
[root@shell_example mnt]# userdel -r user1
[root@shell_example mnt]# sh user_create.sh user_file pass_file
5.使用shell腳本判斷文件的類型
當沒有給文件時,輸出“Please show me a file after script",當文件不存在時,輸出"文件名稱 is not exist",當文件的類型是其他類型時,輸出每個文件的文件類型名稱
編寫一個shell腳本:
[root@shell_example mnt]# vim check_file.sh
腳本內容如下:
#!/bin/bash
[ -z "$1" ] && {
echo "Error: Please show me a file after script"
exit
}
[ -e "$1" ] || {
echo "$1 is not exist"
exit
}
[ -L "$1" ] && {
echo "$1 is a link file"
exit
}
[ -f "$1" ] && {
echo "$1 is a common file"
exit
}
[ -d "$1" ] && {
echo "$1 is a directory"
exit
}
[ -c "$1" ] && {
echo "$1 is a char file"
exit
}
[ -b "$1" ] && {
echo "$1 is a block file"
exit
}
[ -S "$1" ] && {
echo "$1 is a socket file"
exit
}
運行腳本,進行測試:
沒有輸入任何文件時:
[root@shell_example mnt]# sh check_file.sh
當輸入的文件是鏈接文件時:
[root@shell_example mnt]# sh check_file.sh file
當輸入的是一個目錄時:
[root@shell_example mnt]# sh check_file.sh /mnt
當輸入的文件不存在時:
[root@shell_example mnt]# sh check_file.sh file2
當輸入的文件是一個塊設備時:
[root@shell_example mnt]# sh check_file.sh /dev/sda
當輸入的文件是一個字符設備時;
[root@shell_example mnt]# sh check_file.sh /dev/pts/0
當輸入的文件是一個套接字時:
[root@shell_example mnt]# sh check_file.sh /var/lib/mysql/mysql.sock
/var/lib/mysql/mysql.sock is a socket file
6.讓一個文件中所有的內容都變為大寫或者小寫
編輯westos文件:
[root@shell_example mnt]# vim westos
[root@shell_example mnt]# cat westos
hahaLLdodoEH
將westos文件中的內容都變為大寫:
[root@shell_example mnt]# tr 'a-z' 'A-Z' < westos
HAHALLDODOEH
將westos文件中的內容都變為小寫:
[root@shell_example mnt]# tr 'A-Z' 'a-z' < westos
hahalldodoeh