shell Exercise: Record a question every day
1, demand: - Batch change the file name
All files with a .txt extension found under / 123 directory
- Bulk edit .txt is .txt.bak
- All .bak files are packaged compressed 123.tar.gz
- The name of the batch file to restore, that is to increase the .bak delete
#! / bin / bash ## txt file to find the Find / 123 The -type f -name " * .txt " > / tmp / txt.list ## batch modify the file name for f in ` CAT / tmp / txt.list` do Music Videos $ F $ f.bak DONE ## to create a directory, the directory already exists in order to avoid, so to add a complex extension D = ` DATE +% Y% m% D% H% m% S ' mkdir / tmp / $ D 123_ ## .bak copy files to / tmp / 123_ $ D for F in ` CAT / tmp / txt.list` do CPf.bak $ / tmp / 123_ $ D DONE ## packing compression CD / tmp / the tar CZF 123 . the tar .gz 123_ $ D / ## reduction for F in ` CAT / tmp / txt.list` do Music Videos $ F. f $ BAK DONE
2, demands: - the statistical average user
写个shell,看看你的Linux系统中是否有自定义用户(普通用户),若是有,一共有几个?并输出姓名!
Assuming all users are common to more than 1000 uid # ! / Bin / the bash ## find the row with the ordinary user, and count the number of n- = ` awk -F ' : ' ' $. 3> = 1000 ' / etc / the passwd | WC - l` ## determines the number of general users IF [$ n--gt 0 ] the then ## outputs a normal user and how many echo " There are $ n-Common users. " the first line ## to find the average user's line and print ( line with the user's name) the uname = ` awk -F ' : ' ' $. 3> = 1000 ' / etc / the passwd | awk -F ':' '{print $1}'` echo "the user were $uname ." else echo "No common users." fi
3. Requirements: - detect file changes
There are two Linux servers A and B, if A can ssh directly to B, no password. A and B have a directory called / data / web / file down here there are many, of course, we do not know the specific subdirectory of several layers, before the file in that directory if A and B are exactly the same. But now uncertain whether the agreement. Solid we need to write a script to achieve this function, the machine detects similarities and differences between A and B machines / data / web / directory file, a file on our A machine as standard. For example, if one less machine B a.txt file, then we should be able to detect it, or b.txt files on the B machine has changes, we should be able to detect it (more files on machine B we do not consider) .
提示: 使用核心命令 md5sum a.txt 算出md5值,去和B机器上的比较。
#! / bin / the bash # machine A to B is assumed that the machine has done no password set the dir = / Data / Web ## is assumed as an IP machine B 192. 168.0 . 100 B_ip = 192.168 . 0.100 Find $ the dir -type F | xargs md5sum> / tmp / md5.txt SSH $ B_ip " the Find $ dir The -type f | xargs md5sum> /tmp/md5_b.txt " scp $ B_ip: /tmp/md5_b.txt / tmp for f in ` awk ' { $ 2} Print ' / tmp / md5.txt` do IF grep -q"$f" /tmp/md5_b.txt then md5_a=`grep $f /tmp/md5.txt|awk '{print $1}'` md5_b=`grep $f /tmp/md5_b.txt|awk '{print $1}'` if [ $md5_a != $md5_b ] then echo "$f changed." fi else echo "$f deleted. " fi done