Shell练习题(持续更新)
生活随笔
收集整理的這篇文章主要介紹了
Shell练习题(持续更新)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.輸出1-10
echo {1..10} seq -s' ' 1 10 #默認(rèn)分隔符\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;}';echo2.隨機(jī)選人功能(while & 數(shù)組)
比如pick.sh 2,則顯示兩個(gè)人tom sam
#!/bin/bash com=`basename $0` num=$1 #選取人數(shù) member=(sam tom jack marry terry) #定義數(shù)組 total=${#member[@]} #總數(shù)5 if [ $# -lt 1 -o $# -gt 1 ]; then #判斷參數(shù)是否是一個(gè)echo "$com {[1-$total]}"exit 4 fi if [ $num -le $total -a $num -gt 0 ]; then #判斷選取人數(shù)在1-5while [ $num -ge 1 ]; dorand=`echo "$RANDOM%$total" | bc` #取隨機(jī)數(shù)0-4if [ -n "${member[$rand]}" ]; then #判斷數(shù)值是否為空echo -n "${member[$rand]} "unset member[$rand] #刪除數(shù)組值let num--fidone elseecho "$com {[1-$total]}" fi echo3. 查詢某字符串所在行號(hào)
查詢hadoop用戶在/etc/passwd中的行號(hào)
grep -n '^hadoop:' /etc/passwd | cut -d: -f1 awk '/^hadoop:/{print NR}' /etc/passwd4.文件備份(tar & find)
使用tar備份和find刪除大于3天的備份文件
#!/bin/bash list=/tmp/backup.list #備份文件列表 ddir=/tmp/b #備份路徑 day=2 #備份保留天數(shù) 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天前的備份文件 done5.初始化Linux配置文件
用于光安裝好的CentOS 5/6初始化
運(yùn)行腳本前先要把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 -a6.復(fù)制命令至/mnt/test下;以/mnt/test作為根(chroot /mnt/test),進(jìn)行測(cè)試。
#!/bin/bash # cp Command to other # 2014-2-19 TargetDir=/mnt/test [ -d $TargetDir ] || mkdir -p $TargetDir #5秒不輸入,就為默認(rèn)值q,退出 read -t 5 -p "Input A Command: " Command Command=${Command:-q} #循環(huán),直到輸入q或Q后,退出 while [ "$Command" != 'q' -a "$Command" != 'Q' ]; do #復(fù)制命令 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." #復(fù)制命令所用到的庫(kù)文件 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 done7.Oracle VM虛擬機(jī)在線統(tǒng)計(jì)
#!/bin/bash # 統(tǒng)計(jì)oracle vm虛擬機(jī)在線數(shù)量 # 2014-02-20 #列出在線數(shù)量 xm list | grep -v ^Name | grep -v ^Domain-0 | awk '{print $1,$2}' > /backup/onlinevm.txt #列出虛擬機(jī)總數(shù) grep -R "OVM_simple_name" /OVS/Repositories/0004fb0000030000a95eee3c2b21307a/VirtualMachines/* | awk 'BEGIN{FS="/"}{print $6,$7}' | awk '{print $1,$4}' > /backup/allvm.txt #合并兩個(gè)文檔 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 #顯示在線數(shù)量 num=$(cat /backup/onlinevm.txt | wc -l) echo -e "\e[32mTotal $num VM Running.\e[0m"8.mysql-5.5.33通用安裝包安裝
運(yùn)行時(shí),腳本與mysql安裝包放在一個(gè)文件夾下。
#!/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` #創(chuàng)建mysql用戶 id mysql &> /dev/null || useradd -r mysql #創(chuàng)建mysql數(shù)據(jù)庫(kù)文件夾 [ -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 #修改配置,線程數(shù)和CPU個(gè)數(shù)相同 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 #復(fù)***務(wù)器啟動(dòng)文件 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld #安裝mysql yum install -y libaio &> /dev/null #進(jìn)入到mysql文件夾才能,運(yùn)行下面的安裝命令 cd /usr/local/mysql scripts/mysql_install_db --user=mysql --datadir=/mydata/data #配置環(huán)境變量 cat > /etc/profile.d/mysql.sh << EOF export PATH=/usr/local/mysql/bin:\$PATH EOF轉(zhuǎn)載于:https://blog.51cto.com/samlinux/1310704
總結(jié)
以上是生活随笔為你收集整理的Shell练习题(持续更新)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery实现等比例缩放大图片让大图片
- 下一篇: PDF批量删除注释