[sujx@LEGION:~]$ source user_add.sh 请输入用户名:testname 请输入密码: useradd: Permission denied. useradd: cannot lock /etc/passwd; try again later. Only root can do that.
重定向
EOF(end of file)作为分隔符
<< Here Document,代表你需要的内容在这里,程序从here document中读取数据,再通过重定向到文件
[sujx@docker ~]$ touch ver1.txt ver2.txt [sujx@docker ~]$ mkdir test # -e 判断文件或目录是否存在 [sujx@docker ~]$ [ -e test ] && echo Y || echo N Y [sujx@docker ~]$ [ -e ver1.txt ] && echo Y || echo N Y [sujx@docker ~]$ [ ! -e ver1.txt ] && echo Y || echo N N # -f 判断是否为目录 [sujx@docker ~]$ [ ! -f ver1.txt ] && echo Y || echo N N [sujx@docker ~]$ [ -f ver1.txt ] && echo Y || echo N Y [sujx@docker ~]$ [ -f test/ ] && echo Y || echo N N # -b 判断是否为块设备 [sujx@docker ~]$ [ -b /dev/sda ] && echo Y || echo N Y [sujx@docker ~]$ [ -b /etc/passwd ] && echo Y || echo N N # -s 判断文件是否存在并为非空文件 [sujx@docker ~]$ [ -s ver1.txt ] && echo Y || echo N N [sujx@docker ~]$ echo "Hello" > ver1.txt [sujx@docker ~]$ [ -s ver1.txt ] && echo Y || echo N Y
# 现实字符的颜色、背景、显示方式 [sujx@docker ~]$ cat color-menu.sh #!/bin/bash # This is echo color shell # by author rivers 2021.09-23 # 字体颜色 for i in {31..37}; do echo -e "\033[$i;40mHello world!\033[0m" done # 背景颜色 for i in {41..47}; do echo -e "\033[47;${i}mHello world!\033[0m" done # 显示方式 for i in {1..8}; do echo -e "\033[$i;31;40mHello world!\033[0m" done # 测试主机连通性 [sujx@docker ~]$ cat ping_check2.sh #!/bin/bash #功能描述(Description):测试某个网段内所有主机的连通性 #可以使用$()或``对命令进行扩展 net="192.168.10" for i in $(seq 254) do
ping -c2 -i0.2 -W1 $net.$i &>/dev/null if [ $? -eq 0 ];then echo "$net.$i is up." else echo "$net.$i is down." fi done # C语言风格循环 [sujx@docker ~]$ cat c-style.sh #!/bin/bash #功能描述(Description):C 语言风格的 for 循环示例 #i 初始值为 1,j 初始值为 5 #每循环一次对 i 进行自加 1 运算、对 j 进行自减 1 运算,当 i 大于 5 时循环结束 for ((i=1,j=5;i<=5;i++,j--)) do echo "$i $j" done
net="192.168.10" multi_ping() { ping -c2 -i0.2 -W1 $net.$i &>/dev/null if [ $? -eq 0 ];then echo "$1 is up." else echo "$1 is down." fi } for i in {1..254} do
#!/bin/sh # shellcheck disable=SC1090 disable=SC2059 disable=SC2164 disable=SC2181 # setup-repos.sh # Configures Webmin repository for RHEL and Debian systems (derivatives)
webmin_host="download.webmin.com" webmin_download="https://$webmin_host" webmin_key="developers-key.asc" webmin_key_download="$webmin_download/$webmin_key" webmin_key_suffix="webmin-developers" debian_repo_file="/etc/apt/sources.list.d/webmin.list" rhel_repo_file="/etc/yum.repos.d/webmin.repo" download_wget="/usr/bin/wget" download="$download_wget -nv" # Temporary colors NORMAL='' GREEN='' RED='' ITALIC='' BOLD='' if tty -s; then NORMAL="$(tput sgr0)" GREEN=$(tput setaf 2) RED="$(tput setaf 1)" BOLD=$(tput bold) ITALIC=$(tput sitm) fi # Check user permission if [ "$(id -u)" -ne 0 ]; then echo "${RED}Error:${NORMAL} \`setup-repos.sh\` script must be run as root!" >&2 exit 1 fi # Go to temp cd "/tmp" 1>/dev/null 2>&1 if [ "$?" != "0" ]; then echo "${RED}Error:${NORMAL} Failed to switch to \`/tmp\` directory!" exit 1 fi # Check for OS release file osrelease="/etc/os-release" if [ ! -f "$osrelease" ]; then echo "${RED}Error:${NORMAL} Cannot detect OS!" exit 1 fi # Detect OS and package manager and install command . "$osrelease" if [ -n "${ID_LIKE}" ]; then osid="$ID_LIKE" else osid="$ID" fi if [ -z "$osid" ]; then echo "${RED}Error:${NORMAL} Failed to detect OS!" exit 1 fi # Derivatives precise test osid_debian_like=$(echo "$osid" | grep "debian\|ubuntu") osid_rhel_like=$(echo "$osid" | grep "rhel\|fedora\|centos")
repoid_debian_like=debian if [ -n "${ID}" ]; then repoid_debian_like="${ID}" fi # Setup OS dependent if [ -n "$osid_debian_like" ]; then package_type=deb install_cmd="apt-get install --install-recommends" install="$install_cmd --quiet --assume-yes" clean="apt-get clean" update="apt-get update" elif [ -n "$osid_rhel_like" ]; then package_type=rpm if command -pv dnf 1>/dev/null 2>&1; then install_cmd="dnf install" install="$install_cmd -y" clean="dnf clean all" else install_cmd="yum install" install="$install_cmd -y" clean="yum clean all" fi else echo "${RED}Error:${NORMAL} Unknown OS : $osid" exit fi # Ask first if [ "$1" != "-f" ] && [ "$1" != "--force" ]; then printf "Setup repository? (y/N) " read -r sslyn if [ "$sslyn" != "y" ] && [ "$sslyn" != "Y" ]; then exit fi fi # Check for wget or curl or fetch if [ ! -x "$download_wget" ]; then if [ -x "/usr/bin/curl" ]; then download="/usr/bin/curl -f -s -L -O" elif [ -x "/usr/bin/fetch" ]; then download="/usr/bin/fetch" else # Try installing wget echo " Installing required ${ITALIC}wget${NORMAL} package from OS repository .." $install wget 1>/dev/null 2>&1 if [ "$?" != "0" ]; then echo " .. failed to install 'wget' package!" exit 1 else echo " .. done" fi fi fi # Check if GPG command is installed if [ -n "$osid_debian_like" ]; then if [ ! -x /usr/bin/gpg ]; then $update 1>/dev/null 2>&1 $install gnupg 1>/dev/null 2>&1 fi fi # Clean files rm -f "/tmp/$webmin_key" # Download key echo " Downloading Webmin key .." download_out=$($download $webmin_key_download 2>/dev/null 2>&1) if [ "$?" != "0" ]; then download_out=$(echo "$download_out" | tr '\n' ' ') echo " ..failed : $download_out" exit fi echo " .. done" # Setup repos case "$package_type" in rpm) # Install our keys echo " Installing Webmin key .." rpm --import $webmin_key cp -f $webmin_key /etc/pki/rpm-gpg/RPM-GPG-KEY-$webmin_key_suffix echo " .. done" # Create repo file echo " Setting up Webmin repository .." echo "[webmin-noarch]" >$rhel_repo_file echo "name=Webmin - noarch" >>$rhel_repo_file echo "baseurl=$webmin_download/download/newkey/yum" >>$rhel_repo_file echo "enabled=1" >>$rhel_repo_file echo "gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-$webmin_key_suffix" >>$rhel_repo_file echo "gpgcheck=1" >>$rhel_repo_file echo " .. done" ;; deb) # Remove our keys rm -f "/usr/share/keyrings/debian-$webmin_key_suffix.gpg" "/usr/share/keyrings/$repoid_debian_like-$webmin_key_suffix.gpg" # Install our keys echo " Installing Webmin key .." gpg --import $webmin_key 1>/dev/null 2>&1 cat $webmin_key | gpg --dearmor > "/usr/share/keyrings/$repoid_debian_like-$webmin_key_suffix.gpg" echo " .. done" # Remove Webmin repo from sources.list sources_list=$(grep -v "$webmin_host" /etc/apt/sources.list) echo "$sources_list" > /etc/apt/sources.list # Create repo file echo " Setting up Webmin repository .." echo "deb [signed-by=/usr/share/keyrings/$repoid_debian_like-$webmin_key_suffix.gpg] $webmin_download/download/newkey/repository stable contrib" >$debian_repo_file echo " .. done" # Clean meta echo " Cleaning repository metadata .." $clean 1>/dev/null 2>&1 echo " .. done" # Update meta echo " Downloading repository metadata .." $update 1>/dev/null 2>&1 echo " .. done" ;; *) echo "${RED}Error:${NORMAL} Cannot setup repositories on this system." exit 1 ;; esac # Could not setup if [ ! -x "/usr/bin/webmin" ]; then echo "Webmin package can now be installed using ${GREEN}${BOLD}${ITALIC}$install_cmd webmin${NORMAL} command." fi
read -p "请输入日志文件:" logfile echo #统计页面访问量(PV) PV=$(cat $logfile | wc -l) #统计用户数量(UV) UV=$(cut -f1 -d' ' $logfile | sort | uniq | wc -l) #统计人均访问次量 Average_PV=$(echo "scale=2;$PV/$UV" | bc) #统计每个 IP 的访问次数 declare -A IP while read ip other do let IP[$ip]+=1 done < $logfile #统计各种 HTTP 状态码的个数,如 404 报错的次数、500 报错的次数等 declare -A STATUS while read ip dash user time zone method file protocol code size othe do let STATUS[$code]++ done < $logfile #统计网页累计访问字节大小 while read ip dash user time zone method file protocol code size other do let Body_size+=$size done < $logfile #统计热点数据 declare -A URI while read ip dash user time zone method file protocol code size other do let URI[$file]++ done < $logfile echo -e "\033[91m\t 日志分析数据报表\033[0m" #显示 PV 与 UV 访问量,平均用户访问量 $line echo -e "累计 PV 量: $GREEN_COL$PV$NONE_COL" echo -e "累计 UV 量: $GREEN_COL$UV$NONE_COL" echo -e "平均用户访问量: $GREEN_COL$Average_PV$NONE_COL" #显示网页累计访问字节数 $line echo -e "累计访问字节数: $GREEN_COL$Body_size$NONE_COL Byte" #显示指定的 HTTP 状态码数量 $line for i in 200 404 500 do if [ ${STATUS[$i]} ];then echo -e "$i 状态码次数:$GREEN_COL ${STATUS[$i]} $NONE_COL" else echo -e "$i 状态码次数:$GREEN_COL 0 $NONE_COL" fi done #显示每个 IP 的访问次数 line for i in ${!IP[@]} do printf "%-15s 的访问次数为: $GREEN_COL%s$NONE_COL\n" $i ${IP[$i]} done echo #显示访问量大于 500 的 URI echo -e "$GREEN_COL 访问量大于 500 的 URI:$NONE_COL" for i in "${!URI[@]}" do if [ ${URI["$i"]} -gt 500 ];then echo "-----------------------------------" echo "$i" echo "${URI[$i]}次" echo "-----------------------------------" fi done