1.输出1-10
echo {1..10} seq -s' ' 1 10 #默认分隔符\n for((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;echo awk 'BEGIN{for(i=1;i<=10;i++) printf "%s ",i;}';echo
2.随机选人功能(while & 数组)
比如pick.sh 2,则显示两个人tom sam
#!/bin/bash com=`basename $0` num=$1 #选取人数 member=(sam tom jack marry terry) #定义数组 total=${#member[@]} #总数5 if [ $# -lt 1 -o $# -gt 1 ]; then #判断参数是否是一个echo "$com {[1-$total]}"exit 4 fi if [ $num -le $total -a $num -gt 0 ]; then #判断选取人数在1-5while [ $num -ge 1 ]; dorand=`echo "$RANDOM%$total" | bc` #取随机数0-4if [ -n "${member[$rand]}" ]; then #判断数值是否为空echo -n "${member[$rand]} "unset member[$rand] #删除数组值let num--fidone elseecho "$com {[1-$total]}" fi echo
3. 查询某字符串所在行号
查询hadoop用户在/etc/passwd中的行号
grep -n '^hadoop:' /etc/passwd | cut -d: -f1 awk '/^hadoop:/{print NR}' /etc/passwd
4.文件备份(tar & find)
使用tar备份和find删除大于3天的备份文件
#!/bin/bash list=/tmp/backup.list #备份文件列表 ddir=/tmp/b #备份路径 day=2 #备份保留天数 echo "[Backup]" >> /tmp/backup.log echo "start: `date +%F-%T`" >> /tmp/backup.log if [ ! -f $list ]; thenecho "$list doesn't exists."echo -e "[END]\n" >> /tmp/backup.log fi [ -d "$ddir" ] || mkdir -p $ddir &> /dev/null for file in `cat $list`; dotar jcf $ddir/`basename $file`-`date +%F-%H-%M-%M`.tar.bz2 $file &> /dev/nullif [ $? -eq 0 ]; thenecho "File: $file backup successfully." >> /tmp/backup.logelseecho "file: $file backup failed." >> /tmp/backup.logfi done echo "end: `date +%F-%T`" >> /tmp/backup.log echo -e "[Backup end]\n" >> /tmp/backup.log for bakfile in $ddir/*; dofind $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 version version=$(grep -o "[0-9]\.[0-9]" /etc/issue | cut -d . -f1) #shutdown iptables & selinux & NetworkManager iptables -F service iptables save &> /dev/null sed -i s/SELINUX=enabled/SELINUX=disabled/g /etc/selinux/config service NetworkManager stop &> /dev/null #backup etc & yum repo [ -e /backup ] || mkdir /backup tar zcf /backup/etc.tar.gz /etc &> /dev/null find /etc/yum.repos.d/ -type f -exec mv {} /etc/yum.repos.d/old \; &> /dev/null #add yum repo from cdrom if [ "$version" -eq 5 ]; then cat > /etc/yum.repos.d/cdrom.repo << EOF [cdrom] name=local source baseurl=file:///mnt/cdrom/Server gpgcheck=0 enabled=1 EOF elif [ "$version" -eq 6 ]; then cat > /etc/yum.repos.d/cdrom.repo << EOF [cdrom] name=local source baseurl=file:///mnt/cdrom/ gpgcheck=0 enabled=1 EOF fi #mount cdrom [ -e /mnt/cdrom ] || mkdir /mnt/cdrom grep "/dev/cdrom" /etc/fstab &> /dev/null if [ ! $? -eq 0 ]; then cat >> /etc/fstab << EOF /dev/cdrom /mnt/cdrom/ iso9660 defaults,loop,ro 0 0 EOF fi mount -a
6.复制命令至/mnt/test下;以/mnt/test作为根(chroot /mnt/test),进行测试。
#!/bin/bash # cp Command to other # 2014-2-19 TargetDir=/mnt/test [ -d $TargetDir ] || mkdir -p $TargetDir #5秒不输入,就为默认值q,退出 read -t 5 -p "Input A Command: " Command Command=${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:]]*"`; do LIBDir=${LIB%/*} [ -d ${TargetDir}/${LIBDir} ] || mkdir -p ${TargetDir}/${LIBDir} [ -f ${TargetDir}/${LIB} ] || cp $LIB ${TargetDir}/${LIB} && echo "Copy $LIB to $Target finished." done read -p "Input your command: " Command done
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.txt awk '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-21 Software=mysql-5.5.33-linux2.6-x86_64.tar.gz MysqlDir=/usr/local/mysql DataDir=/mydata/data CpuNum=`cat /proc/cpuinfo | grep processor | wc -l` #创建mysql用户 id mysql &> /dev/null || useradd -r mysql #创建mysql数据库文件夹 [ -d /mydata/data ] || mkdir -p /mydata/data chown mysql.mysql -R /mydata/data #安装mysql tar xf $Software -C /usr/local ln -s /usr/local/mysql-5.5.33-linux2.6-x86_64 $MysqlDir chown 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/data sed -i '/\[mysqld\]/adatadir = /mydata/data' /etc/my.cnf #复***务器启动文件 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld #安装mysql yum install -y libaio &> /dev/null #进入到mysql文件夹才能,运行下面的安装命令 cd /usr/local/mysql scripts/mysql_install_db --user=mysql --datadir=/mydata/data #配置环境变量 cat > /etc/profile.d/mysql.sh << EOF export PATH=/usr/local/mysql/bin:\$PATH EOF
转载于:https://blog.51cto.com/samlinux/1310704