Redhat_rh033_j

Shell脚本语言:
1.)for循环:
           语句格式:for  variable  in  value1  value2 ...
                     do
                          command  using  $variable
                     done
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/bin/bash
#for_sum.sh
#Author:Our RHCE Team
#version:0.0.0.1
#计算1-100的和
let SUM=0
 for I  in {1..100}
do
  SUM=$[$SUM+$I]
done
echo "The SUM is $SUM."
**********************************************************************************
#!/bin/bash
#for_file.sh
#Author:Our RHCE Team
#version:0.0.0.1
#查看某一目录中所有文件的属性信息
for FILE in  $1/*
do
  file $FILE
done
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2.)while循环:
           语法格式:while[ conditions ]
                     do
                           command
                           ...
                     done
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/bin/bash
#for.sh
#Author:Our RHCE Team
#version:0.0.0.1
#给文件加行号并且每行有分隔符
FILE2=`mktemp /tmp/tmp.XXXXXX`
echo "Please enter a file:"
read FILE
let COUNT=1
NUM=`cat $FILE | wc -l`
if [ -e $FILE -a -f $FILE  ];then
  while read LINE
    do
       if [ "$COUNT" -lt "$NUM" ];then
         echo -e "$COUNT #$LINE\n-----------------" >>$FILE2
        else
         echo -e "$COUNT #$LINE" >>$FILE2
       fi   
       COUNT=$(($COUNT+1))
    done <  $FILE
     cat $FILE2
fi
rm -f $FILE2
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3.)until循环:
           语法格式:until[ conditions ]
                     do
                           command
                           ...
                     done
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/bin/bash
#until.sh
#Author:Our RHCE Team
#version:0.0.0.1
#ping192.168.0网段内的主机某台主机
ping_function() {
let I=1
until [ $I -gt "254" ]
do
      host=192.168.0.$I
      ping -c2 $host  &>  /dev/null
       if [ $? -eq 0 ];then
          echo "$host is online"
       else
          echo "$host isn't online"
       fi
      I=$(($I+1))
done
}
ping_function
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4.)if条件语句:
            语法格式:if [  conditions  ];then
                              command
                      else
                              command
                      fi
                      if [  conditions  ];then
                              command
                      elif [  conditions  ];then
                              command
                      else
                              commamd
                      fi
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/bin/bash
#if.sh
#Author:Our RHCE Team
#version:0.0.0.1
#查看并修改ip地址
change_ip() {
 sed -i 's/^BOOTPROTO=.*/BOOTPROTO=static/'  $1
if grep "^IPADDR" $1  &> /dev/null ;then
   sed  -i 's/^IPADDR=.*/IPADDR='$2'/'  $1
else
   sed  -i '/^BOOTPROTO=static/a \IPADDR='$2'' $1
fi 
if grep "^NETMASK" $1  &> /dev/null ;then
   sed  -i 's/^NETMASK=.*/NETMASK='$3'/'  $1
else
   sed  -i '/^IPADDR=/a \NETMASK='$3'' $1
fi
cat $1
}
read -p "please choose your network,IP and netmask:" NET IP_ADDR NTMK
change_ip $NET $IP_ADDR $NTMK
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5.)case选择语句:
             语法格式:case  "$variable" in
                       "condition1")
                                   command
                                   ...
                                   ;;
                       "condition2")
                                   command
                                   ...
                                   ;;
                       * )
                                   command
                                   ...
                                   ;;
                       esca
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#!/bin/bash
#case.sh
#Author:Our RHCE Team
#version:0.0.0.1
#用户选择+-*/并输入两个数字得出计算结果
MENU=`mktemp /tmp/menu.XXXXXX`
cat>>$MENU<<EOF
==================================
|            MENU                 |
|          1.sum                  |
|          2.minus                |
|          3.multi                |
|          4.divid                |
==================================
EOF
cat $MENU   
let O=0
let A=0
let B=0
let result=0
echo "please choose one number from 1~4:"
read O
if [ ! $O -eq "1" ] && [ ! $O -eq "2" ] && [ ! $O -eq "3" ] && [ ! $O  -eq "4" ];then
 echo "GAME OVER!!!"
 rm -f  $MENU
 exit 1
fi
echo "please put in two int:"
read A
read B
case $O  in
   "1")
       result=$[$A+$B]
       echo "The sum is $result"
       ;;
   "2")
       result=$[$A-$B]
       echo "The minus is $result"
       ;;
   "3")
       result=$[$A*$B]
       echo "The multi is $result"
       ;;
   "4")
       result=$[$A/$B]
       echo "The divid is $result"
       ;;
esac
rm -f  $MENU
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
set            所有变量包括所有的环境变量和本地变量
env            所有的环境变量
普通变量的声明:echo $VAR ;unset VAR
环境变量的声明:export A,declare -x A ;unset A
修改全局的:    /etc/profile;/etc/profile.d/*;/etc/bashrc
用户家目录内的:/.bash_profile ;/.bashrc 
登录shell:
/etc/profile-->/.bash_profile-->/etc/profile.d/*-->/.bashrc -->/etc/bashrc
非登录shell:
/.bashrc-->/etc/bashrc-->/etc/profile.d/*
profile主要有环境变量,写用户进程执行的程序;bashrc主要有本地变量,给命令写别名。
 

sed是一个行编辑器,它逐行处理要被处理对象文件的内容,并会把结果输送至屏幕,常用的sed命令:
s///   s@@@(分隔符)                 "/"和"@"代表了分隔符
sed -n '/STRING/p'  filex                  把文件filex中的所有包含STRING的行打印在屏幕上,filex的内容不会改变;如果没有-n选项,所有包含SREING的行都会被打印两次。
sed '1,3d'  filex >newfilex                删除文件filex的前三行,将修改结果保存到newfilex文件中
sed '/[Dd]aniel/d'  filex                  删除包含Daniel或daniel的行
sed -n '/15,20/p'   filex                  只打印第15-20行
sed '1,10s/Montana/MT/g'  filex            将第1-10行的所有Montana全局替换为MT
sed '/March/\!d'  filex(csh)               删除所有不含March的行,需转义历史字符
sed '/March/!d'   filex(sh)                删除所有不含March的行 
sed '/report/s/5/8/'  filex                把所有包含report的行里出现第一个5改成8
sed 's/^....//'   filex                    删除每行的前4个字符 
sed 's/...$//'    filex                    删除每行的后3个字符
sed '/east/,/west/s/North/South/' filex    把从east到west这个范围内所有行中出现的North替换为South
sed -n '/Time off/w timefile'     filex    见所有包含Time off的行写入到timefile文件中  
sed 's/\([Oo]ccur\)ence/\1rence/' filex    将所有Occurence替换成Occurence;occurence替换成occurrence
sed [-n(不显示缓冲池<模式空间>的内容)] '/root/p'  /etc/group 
y  (不支持正则表达式)
s与y的区别:
s/ab/AB        用"AB"字符替换所有的"ab"
y/ab/AB        用"A"字符替换"a";"B"字符替换"b"
 

监控登入:
who:               查看那些用户登入系统
w:                 显示但当前登入进来的用户,以及用户在干什么
sleep:             暂停一段时间  sleep num [SUFFIX]  num代表了暂停几秒钟;SUFFIX项有d,m,s三个选项
last:               搜索/var/log/wtmp内的所有用户的登入信息
last -n 3           显示前三条的登入信息
last -n 3 -i        把主机名称形式转换到ip地址
last -x             级别切换的过程(主机开关机的记录)
lastb               尝试登入但没有成功的记录
lastlog             所有用户最近的登入信息
lastlog  -u  root   显示该用户的最近登入信息