1.输出1-10
echo {1..10}seq -s' ' 1 10 #默认分隔符\nfor((i=1;i<=10;i++));do echo -n "$i ";done;echo #最后echo为了换行i=1;while [ $i -le 10 ];do printf "%s " $i;i=$[$i+1];done;echoawk 'BEGIN{for(i=1;i<=10;i++) printf "%s ",i;}';echo
2.随机选人功能(while & 数组)
比如pick.sh 2,则显示两个人tom sam
#!/bin/bashcom=`basename $0`num=$1 #选取人数member=(sam tom jack marry terry) #定义数组total=${#member[@]} #总数5if [ $# -lt 1 -o $# -gt 1 ]; then #判断参数是否是一个 echo "$com {[1-$total]}" exit 4fiif [ $num -le $total -a $num -gt 0 ]; then #判断选取人数在1-5 while [ $num -ge 1 ]; do rand=`echo "$RANDOM%$total" | bc` #取随机数0-4 if [ -n "${member[$rand]}" ]; then #判断数值是否为空 echo -n "${member[$rand]} " unset member[$rand] #删除数组值 let num-- fi doneelse echo "$com {[1-$total]}"fiecho
3. 查询某字符串所在行号
查询hadoop用户在/etc/passwd中的行号
grep -n '^hadoop:' /etc/passwd | cut -d: -f1awk '/^hadoop:/{print NR}' /etc/passwd
4.文件备份(tar & find)
使用tar备份和find删除大于3天的备份文件
#!/bin/bashlist=/tmp/backup.list #备份文件列表ddir=/tmp/b #备份路径day=2 #备份保留天数echo "[Backup]" >> /tmp/backup.logecho "start: `date +%F-%T`" >> /tmp/backup.logif [ ! -f $list ]; then echo "$list doesn't exists." echo -e "[END]\n" >> /tmp/backup.logfi[ -d "$ddir" ] || mkdir -p $ddir &> /dev/nullfor file in `cat $list`; do tar jcf $ddir/`basename $file`-`date +%F-%H-%M-%M`.tar.bz2 $file &> /dev/null if [ $? -eq 0 ]; then echo "File: $file backup successfully." >> /tmp/backup.log else echo "file: $file backup failed." >> /tmp/backup.log fidoneecho "end: `date +%F-%T`" >> /tmp/backup.logecho -e "[Backup end]\n" >> /tmp/backup.logfor bakfile in $ddir/*; do find $ddir -mtime +$day -exec rm $ddir/$bakfile {} \; #删除3天前的备份文件done
5.初始化Linux配置文件
用于光安装好的CentOS 5/6初始化
运行脚本前先要把Linux光盘放入
#!/bin/bash# init linux config# 2014-02-17#chech os versionversion=$(grep -o "[0-9]\.[0-9]" /etc/issue | cut -d . -f1)#shutdown iptables & selinux & NetworkManageriptables -Fservice iptables save &> /dev/nullsed -i s/SELINUX=enabled/SELINUX=disabled/g /etc/selinux/configservice NetworkManager stop &> /dev/null#backup etc & yum repo[ -e /backup ] || mkdir /backuptar zcf /backup/etc.tar.gz /etc &> /dev/nullfind /etc/yum.repos.d/ -type f -exec mv {} /etc/yum.repos.d/old \; &> /dev/null#add yum repo from cdromif [ "$version" -eq 5 ]; thencat > /etc/yum.repos.d/cdrom.repo << EOF[cdrom]name=local sourcebaseurl=file:///mnt/cdrom/Servergpgcheck=0enabled=1EOFelif [ "$version" -eq 6 ]; thencat > /etc/yum.repos.d/cdrom.repo << EOF[cdrom]name=local sourcebaseurl=file:///mnt/cdrom/gpgcheck=0enabled=1EOFfi#mount cdrom[ -e /mnt/cdrom ] || mkdir /mnt/cdromgrep "/dev/cdrom" /etc/fstab &> /dev/nullif [ ! $? -eq 0 ]; thencat >> /etc/fstab << EOF/dev/cdrom /mnt/cdrom/ iso9660 defaults,loop,ro 0 0EOFfimount -a
6.复制命令至/mnt/test下;以/mnt/test作为根(chroot /mnt/test),进行测试。
#!/bin/bash# cp Command to other# 2014-2-19TargetDir=/mnt/test[ -d $TargetDir ] || mkdir -p $TargetDir#5秒不输入,就为默认值q,退出read -t 5 -p "Input A Command: " CommandCommand=${Command:-q}#循环,直到输入q或Q后,退出while [ "$Command" != 'q' -a "$Command" != 'Q' ]; do#复制命令Command=`which $Command | grep -v "^alias" | grep -o [^[:space:]].*`ComDir=${Command%/*}[ -d ${TargetDir}/${ComDir} ] || mkdir -p ${TargetDir}/${ComDir}[ -f ${TargetDir}/${Command} ] || cp $Command ${TargetDir}/${Command} && echo "Copy $Command to $TargetDir finished."#复制命令所用到的库文件for LIB in `ldd $Command | grep -o "[^[:space:]]*/lib[^[:space:]]*"`; doLIBDir=${LIB%/*}[ -d ${TargetDir}/${LIBDir} ] || mkdir -p ${TargetDir}/${LIBDir}[ -f ${TargetDir}/${LIB} ] || cp $LIB ${TargetDir}/${LIB} && echo "Copy $LIB to $Target finished."doneread -p "Input your command: " Commanddone
7.Oracle VM虚拟机在线统计
#!/bin/bash# 统计oracle vm虚拟机在线数量# 2014-02-20#列出在线数量xm list | grep -v ^Name | grep -v ^Domain-0 | awk '{print $1,$2}' > /backup/onlinevm.txt#列出虚拟机总数grep -R "OVM_simple_name" /OVS/Repositories/0004fb0000030000a95eee3c2b21307a/VirtualMachines/* | awk 'BEGIN{FS="/"}{print $6,$7}' | awk '{print $1,$4}' > /backup/allvm.txt#合并两个文档join /backup/onlinevm.txt /backup/allvm.txt > /backup/test.txtawk 'BEGIN{printf("\033[1;31m%-35s%-5s%-5s\033[0m\n","UUID","ID","HOST")};{printf("%-35s%-5s%-5s\n",$1,$2,$3)}' /backup/test.txt#显示在线数量num=$(cat /backup/onlinevm.txt | wc -l)echo -e "\e[32mTotal $num VM Running.\e[0m"
8.mysql-5.5.33通用安装包安装
运行时,脚本与mysql安装包放在一个文件夹下。
#!/bin/bash# 通用包格式安装mysql# 2014-02-21Software=mysql-5.5.33-linux2.6-x86_64.tar.gzMysqlDir=/usr/local/mysqlDataDir=/mydata/dataCpuNum=`cat /proc/cpuinfo | grep processor | wc -l`#创建mysql用户id mysql &> /dev/null || useradd -r mysql#创建mysql数据库文件夹[ -d /mydata/data ] || mkdir -p /mydata/datachown mysql.mysql -R /mydata/data#安装mysqltar xf $Software -C /usr/localln -s /usr/local/mysql-5.5.33-linux2.6-x86_64 $MysqlDirchown root.mysql -R ${MysqlDir}/*cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf -f#修改配置,线程数和CPU个数相同sed -i s/"thread_concurrency = 8"/"thread_concurrency = $CpuNum"/ /etc/my.cnf#修改配置,在[mysql]后插入adatadir = /mydata/datased -i '/\[mysqld\]/adatadir = /mydata/data' /etc/my.cnf#复***务器启动文件cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqldchkconfig --add mysqld#安装mysqlyum install -y libaio &> /dev/null#进入到mysql文件夹才能,运行下面的安装命令cd /usr/local/mysqlscripts/mysql_install_db --user=mysql --datadir=/mydata/data#配置环境变量cat > /etc/profile.d/mysql.sh << EOFexport PATH=/usr/local/mysql/bin:\$PATHEOF