a shell script example

http://felixgers.de/teaching/shells/exercise_shells.html

exercise b:

Writhing Scripts
Write a script that replaces all "*.html" file names 
with "*.html.old" in the current directory.

solution:

#!/bin/bash
#####################################################
#description:
#    this script will replace all "*.html" file name
#    with "*.html.old"
#####################################################

for i in $( ls | grep "\.html$" )
do
    mv "$i" "${i}.old"
done

这很简单。

咱们reverse了一下:

Writhing Scripts
Write a script that replaces all "*.html.old" file names 
with "*.html" in the current directory.

solution:

#!/bin/bash
########################################################
#description:
#    this script will replace all "*.html.old" file name
#    with "*.html"
########################################################

for i in $( ls | grep "\.html\.old$" )
do
    count=$( echo $i | sed 's/\./ /g' | wc -w )
    newname=$( echo $i | cut -d '.' -f 1-"$(( count-1 ))" )
    mv $i $newname
done

转载于:https://my.oschina.net/u/1000140/blog/115879

你可能感兴趣的:(a shell script example)