学习shell——编程练习

#!/bin/bash
read -p "input users: " U
if id $U &> /dev/null
then
echo "$U ok "
else
useradd $U
echo "$U add success"
fi
=============================================================
#!/bin/bash
if [ -f /etc/passwd ]
then 
echo yes
else 
echo no
fi
==============================================================
#!/bin/bash
if [ -d /tmp/a/b/c/d/e ]
then
echo yes
else
mkdir -p /tmp/a/b/c/d/e
fi
=============================================================
for i in /etc/hosts /etc/passwd /tmp/test{1..50}
do if [  ! -f $i ]
then
touch $i
fi
done
=================画斜线=============================================
#!/bin/bash
read -p "input munber: " num
for i in $(seq 1 $num)
do 
for x in $(seq 1 $(($num-$i)))
do 
echo -n " "
done
echo "*"
done
==================金字塔==============================================
#!/bin/bash
read -p "input number: " num
for i in $(seq 1 $num)
do
for x in $(seq 1 $(($num-$i)))
do
echo -n " "
done
for x in $(seq 1 $((2*$i-1)))
do
echo -n "*"
done
echo
done
===================三角形============================================
#!/bin/bash
read -p "input number: " num
for i in $(seq 1 $num)
do
for x in $(seq 1 $(($num-$i)))
do
echo -n " "
done
for x in $(seq 1 $i)
do
echo -n "*"
done
echo
done
================加减乘法口诀=======================================
#!/bin/bash
echo 加法口诀
for i in $(seq 1 9)
do
for n in $(seq 1 $i)
do
m=$(($i+$n))
echo -n "$i+$n=$m"
done
echo
done
echo 减法口诀
for i in $(seq 1 9)
do
        for n in $(seq 1 $i)
        do
                m=$(($i-$n))
                echo -n "$i-$n=$m "
        done
echo
done
echo 乘法口诀
for i in $(seq 1 9)
do
        for n in $(seq 1 $i)
        do
                m=$(($i*$n))
                echo -n "$i*$n=$m "
        done
echo
done
===============猜数字游戏===================================
#!/bin/bash
num=$(($RANDOM%100))
for i in {1..30}
do
read -p" try $i input a number: " num1
# echo the $i number
if [ $num -gt $num1 ]
then
echo your number smaller
elif [ $num -eq $num1 ]
then
echo you are win
exit
else
echo your number bigger
fi
done
=============累加==========================================
#!/bin/bash
read -p "input number: " num
s=0
i=0
until [ $i -ge $num ]
do 
i=$(($i+1))
s=$(($s+$i))
done
echo $s
=====================================================================
#!/bin/bash
while read username uid password group g1 g2
do
if ! grep $group /etc/group | cut -d -f1 |grep ^$group$ &> /dev/null
then
groupadd $group
fi
if ! grep $g1 /etc/group | cut -d -f1 |grep
^$g1$ &> /dev/null
        then
                groupadd $g1


        fi
if ! grep $g2 /etc/group | cut -d -f1 |grep
^$g2$ &> /dev/null
        then
                groupadd $g2


        fi



if id $user &> /dev/null
then
userdel -r $username
fi
useradd -u $uid -g $group -G $g1, $g2 $username
echo $password | passwd --stdin $username &> /dev/null
done < /tmp/userlist
==================阶乘==============================================
#!/bin/bash
i=0
s=1
read -p "input number: " n
if [ $n -eq 0 ]
then
echo 0
exit
else
while [ $i -lt $n ]


do
i=$(($i+1))
s=$(($s*$i))
done
echo $s
fi
=====================case查看相关信息
#!/bin/bash
while true
do
read -p "input a word: " req
case $req in
m)
free ;;
d)
df -h ;;
s)
cat /etc/redhat-release ;;
r)
uname -r ;;
a)
rpm -qa|wc -l ;;
q)
exit ;;
*)
echo error;;

esac
done
======================输出1-100能被5整除但不能被7整除的整数==========
#!/bin/bash
for i in {1..100}
do
if  [ $(($i%5)) -eq 0 -a $(($i%7)) -ne 0 ]
then
echo $i
else
continue
fi
done
==================位置参数case查看相关信息=============================
#!/bin/bash
#read -p "input a word: " req
until [ $# -eq 0 ]
do
#echo $1 $2
case $1 in
-m)
free ;;
-d)
df -h ;;
-s)
cat /etc/redhat-release ;;
-r)
uname -r ;;
-a)
rpm -qa|wc -l ;;
-q)
exit ;;
*)
echo error;;
esac
shift
done
=================软件锁,该bash同一时间只允许一个进程运行============
#!/bin/bash
if [ -e /tmp/.lockfile ]
then
MY_PID=$(cat /tmp/.lockfile)
ps -p $MY_PID &> /dev/null
[ $? -eq 0 ] && echo "is running"&& exit
fi
echo $$ > /tmp/.lockfile
read

rm -f /tmp/.lockfile
=========================2016-08-23 ----END=========================
=========================check ip address============================
#!/bin/bash
A=$1
if [[ $A =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]
then
        for i in $(echo $A | tr '.' ' ')
        do
                if [ $i -gt 255 ]
                then
                        echo no && exit
                fi
        done
        echo yes
else
        echo no
fi
==========================backup=======================================
#!/bin/bash
DIR="/tmp/test"
BAK="$DIR/backup/"
SRC="$DIR/source/"
TODAY=$(date +"%F %H:%M:%S")
FILELIST="$DIR/filelist.txt"
TIME_FILE="$DIR/time_key"


FULL () {
touch $TIME_FILE
find $SRC -type f > $FILELIST
mkdir -p $BAK/"$TODAY" 
tar -T $FILELIST -czf - | tar -xzf - -C $BAK/"$TODAY"
sed -i "s/$/\t$TODAY/" $FILELIST
}


UPDATE () {
  touch $TIME_FILE.new
  find $SRC -type f -newer $TIME_FILE > $DIR/updatefilelist.txt
  mkdir -p $BAK/"$TODAY"
  tar -T $DIR/updatefilelist.txt -czf - | tar -xzf - -C $BAK/"$TODAY"
  /bin/mv $TIME_FILE.new $TIME_FILE
  while read F_NAME
  do 
STR=$(grep "^$F_NAME\>" $FILELIST)
if [ -z "$STR" ]
then
echo -e "$F_NAME\t$TODAY" >> $FILELIST
else
sed -i "s%$STR%$F_NAME\t$TODAY%" $FILELIST
fi
  done < $DIR/updatefilelist.txt


}
case $1 in 
full)
FULL;;
update)
UPDATE;;
*)
echo "bash $0 (full|update)";;
esac
===========================abnum========================================
#!/bin/bash
init_password()
{
        PASSWORD=`echo $(($RANDOM%10000))`
        echo $PASSWORD | grep '^[0-9]\{4\}$' >/dev/null 2>&1
        if [ $? -ne 0 ]
        then
                init_password
        else
                input
        fi
}
input()
{
        echo -n "please input echo $PASSWORD a number between 0000-9999:"
        read INPUT
        echo $INPUT | grep '^[0-9]\{4\}$' &>/dev/null 
        if [ $? -ne 0 ]
        then
                echo "input error,retry a number between 0000-9999 "
                input
        else
                judge
        fi
}
judge()
{
        X=$INPUT
        Y=$PASSWORD
        while [ $INPUT != $PASSWORD ]
        do
                A=0
                B=0
                judge_a
                judge_b
                LOOP=$(( $LOOP + 1))
                echo "****************************"
                echo "*           "$A"A"$B"B     *"
                echo "****************************"
                input
        done
}


judge_a() {
for i in $(seq 1 4)
do
        VAR_INPUT=${X:$(($i-1)):1}
        for j in $(seq 1 4)
        do
                VAR_PASSWORD=${Y:$(($j-1)):1}
        if [[ $VAR_INPUT = $VAR_PASSWORD && $VAR_INPUT != "" && $VAR_PASSWORD != "" && $i = $j ]]
        then
                A=$(($A+1))
                X=${X:0:$(($i-1))}${X:$i:3}
                Y=${Y:0:$(($i-1))}${Y:$i:3}
                judge_a
        fi
        done
done
}
judge_b() {
for i in $(seq 1 4)
do
        VAR_INPUT=${X:$(($i-1)):1}
        for j in $(seq 1 4)
        do
                VAR_PASSWORD=${Y:$(($j-1)):1}
        if [[ $VAR_INPUT = $VAR_PASSWORD && $VAR_INPUT != "" && $VAR_PASSWORD != "" ]]
        then
                B=$(($B+1))
                X=${X:0:$(($i-1))}${X:$i:3}
                Y=${Y:0:$(($j-1))}${Y:$j:3}
                judge_b
        fi
        done
done


}




LOOP=1
init_password
echo "The password is $PASSWORD !"
===================================2016 -08-24 -----END======================
============================httpd=============================================
#!/bin/bash
WWW_NAME=$1
MAIN_CONF=/etc/httpd/conf/httpd.conf
TEST_CONF=/etc/httpd/conf.d/$WWW_NAME.com.conf
ROOT_DIR=/var/www/html/$WWW_NAME.com
#show usage for this script
USAGE () {
if [ -z $1 ]
then
        echo "Usage : bash $0 name"
        exit
fi
}
#.* httpd installed or not
HTTPD_INSTALL () {
if ! rpm -q httpd &> /dev/null
then
yum -y install httpd &> /dev/null
fi
}


NAME_VIRT_OPEN () {
if ! grep ^NameVirtual $MAIN_CONF &>/dev/null 
then
echo "NameVirtualHost *:80" >> $MAIN_CONF
fi
}


CONF_CREATE () {
if [ ! -e $TEST_CONF ]
then
cat >> $TEST_CONF << ENDF
<VirtualHost *:80>
    ServerAdmin root@$WWW_NAME.com
    DocumentRoot $ROOT_DIR
    ServerName www.$WWW_NAME.com
    ErrorLog logs/$WWW_NAME.com-error_log
    CustomLog logs/$WWW_NAME.com-access_log common
</VirtualHost>
ENDF
fi
}


DIR_CREATE () {
if [  ! -d $ROOT_DIR ]
then
mkdir $ROOT_DIR &>/dev/null
cp /tmp/index.html $ROOT_DIR &>/dev/null
fi
}


PD_NAME () {
local num=$(echo $WWW_NAME | awk -F. '{print NF}')
if [ $num -gt 1 ]
then
        echo "Usage : bash $0 name"
        exit
fi
}
PD_NAME
USAGE $1
HTTPD_INSTALL
NAME_VIRT_OPEN
CONF_CREATE
DIR_CREATE
service httpd restart &> /dev/null 
=========================httpd1==========================
#!/bin/bash
HTTP_MAIN_CONF="/etc/httpd/conf/httpd.conf"
DOMAIN="carol.com"
CONFD="/etc/httpd/conf.d/$DOMAIN.conf"
DIRROOT="/var/www/html/$DOMAIN"
SERVERNAME="www.$DOMAIN"


RPM_CHECK() {
if ! rpm -q httpd &> /dev/null
then
yum -y install httpd &> /dev/null
fi
}


CONF_CHECK () {
if ! grep "NameVirtualHost" $HTTP_MAIN_CONF  | grep -v ^# &> /dev/null
then
echo "NameVirtualHost *:80" >> $HTTP_MAIN_CONF
fi


cat > $CONFD << END
<VirtualHost *:80>
    ServerAdmin root@$DOMAIN
    DocumentRoot $DIRROOT
    ServerName $SERVERNAME
    ErrorLog logs/$DOMAIN-error_log
    CustomLog logs/$DOMAIN-access_log common
</VirtualHost>
END
}


DIR_CHECK() {
mkdir -p $DIRROOT &> /dev/null
echo $DOMAIN > $DIRROOT/index.html 
}
SERVICE_CHECK() {
service httpd restart  &> /dev/null
}


#+++++++++++++++++++++++++++++++++++++
# install rpm
RPM_CHECK
# config http
CONF_CHECK
# make documentroot
DIR_CHECK
# start httpd
SERVICE_CHECK
=========================maopao==========
#!/bin/bash
x=$(($#-1))
num=($@)
echo $x ${num[@]}
for i in $(seq 0 2)
do
for j in $(seq 0 $x)
do
if [ ${num[$i]} -gt ${num[$j]} ]
then
temp=${num[$i]}
num[$i]=${num[$j]}
num[$j]=$temp
fi
done
done
echo ${num[@]}
=================================================
posted @ 2016-08-31 23:26  希曼博客  阅读(249)  评论(0编辑  收藏  举报