12.ตัวอย่างสคริปต์

12.1 ตัวอย่างสคริปต์ดูรายชื่อไฟล์ในไดเรกทอรีย่อย

#!/bin/bash

function listdir {
    local PAT="$1"
    local ROOT="$2"
    for i in *; do
        if [ -d "$i" ]; then
            local CUR="$ROOT/$i"
            pushd "$i" &>/dev/null
            listdir "$PAT" "$CUR"
            popd &>/dev/null
        fi
    done
    if [ ! -z "$( ls -d $PAT 2>/dev/null )" ]; then
        echo "Directory: $ROOT"
        ls -d $PAT 2>/dev/null
        echo 
    fi
}

if [ -z "$1" ]; then
   echo List file in PATTERN recursively into directories.
   echo Usage: $0 "PATTERN"
   exit
fi
PATTERN="$1"
echo "List $PATTERN"
listdir "$PATTERN" "."

ให้ผลคล้ายคำสั่ง

$ find * -name PATTERN

12.2 ตัวอย่างสคริปต์บีบอัดสำรองข้อมูล

#!/bin/bash          
SRCD="/home/"
TGTD="/var/backups/"
OF=home-$(date +%Y%m%d).tgz
tar -cZf $TGTD$OF $SRCD

12.3 เปลี่ยนชื่อไฟล์ทีละหลายไฟล์

#!/bin/sh
# renna: rename multiple files according to several rules
# written by felix hudson Jan - 2000

#first check for the various 'modes' that this program has
#if the first ($1) condition matches then we execute that portion of the
#program and then exit

# check for the prefix condition
if [ $1 = p ]; then

#we now get rid of the mode ($1) variable and prefix ($2)
  prefix=$2 ; shift ; shift

# a quick check to see if any files were given
# if none then its better not to do anything than rename some non-existent
# files!!

  if [$1 = ]; then
     echo "no files given"
     exit 0
  fi

# this for loop iterates through all of the files that we gave the program
# it does one rename per file given
  for file in $*
  do
    mv ${file} $prefix$file
  done

#we now exit the program
  exit 0
fi

# check for a suffix rename
# the rest of this part is virtually identical to the previous section
# please see those notes
if [ $1 = s ]; then
  suffix=$2 ; shift ; shift

  if [$1 = ]; then
    echo "no files given"
    exit 0
  fi

  for file in $*
  do
    mv ${file} $file$suffix
  done

  exit 0
fi

# check for the replacement rename
if [ $1 = r ]; then

  shift

# i included this bit as to not damage any files if the user does not specify
# anything to be done
# just a safety measure

  if [ $# -lt 3 ] ; then
    echo "usage: renna r [expression] [replacement] files... "
    exit 0
  fi

# remove other information
  OLD=$1 ; NEW=$2 ; shift ; shift

# this for loop iterates through all of the files that we give the program
# it does one rename per file given using the program 'sed'
# this is a simple command line program that parses standard input and
# replaces a set expression with a give string
# here we pass it the file name ( as standard input) and replace the nessesary
# text

  for file in $*
  do
    new=`echo ${file} | sed s/${OLD}/${NEW}/g`
    mv ${file} $new
  done
  exit 0
fi

# if we have reached here then nothing proper was passed to the program
# so we tell the user how to use it
echo "usage;"
echo " renna p [prefix] files.."
echo " renna s [suffix] files.."
echo " renna r [expression] [replacement] files.."
exit 0

# done!

12.4 เปลี่ยนชื่อไฟล์แบบง่าย

#!/bin/bash
# renames.sh
# basic file renamer

criteria=$1
re_match=$2
replace=$3

for i in $( ls *$criteria* ); 
do
    src=$i
    tgt=$(echo $i | sed -e "s/$re_match/$replace/")
    mv $src $tgt
done

bash: ประยุกต์ทำบัตรเลขที่ลำดับ

มีโจทย์คือ ให้สร้างบัตรแบบเรียงเลขที่ ตั้งแต่ ๑ จนถึง ๕๐๐
ตัดสินใจใช้ bash + inkscape เพราะง่ายดี
โดยสมมุติว่าเราได้สร้างเอกสารเป็นไฟล์ inkscape ไว้แล้ว โดยให้ตัวเลขที่จะเรียงให้มีค่าเป็น XXXX เพื่อให้สะดวกในการใช้คำสั่ง grep

โค๊ดที่สร้างขึ้น ทำแบบง่าย ๆ คือรันตัวเลขตั้งแต่ 1-500 แต่รุ่นนี้เป็นรุ่นทดสอบ ทำแค่ 1-10 พอ ขั้นตอนคือ

  • แปลงตัวเลขเป็นเลขไทย ด้วยคำสั่ง awk
  • นำไปแทนที่ในไฟล์ inkscape แล้วทำออกมาเป็นไฟล์ใหม่ ด้วยคำสั่ง sed
  • แล้วสั่งส่งออกมาเป็นไฟล์นามสกุล png ด้วยคำสั่งแบบบรรทัดคำสั่งของ inkscape เอง ด้วยพารามิเตอร์ -e

โค๊ดมีดังนี้

$ vi runcard
#!/bin/bash

FILE='card.svg'
QUAN=10
MARKER='XXXX'

for i in $(seq 1 $QUAN); do
    TNUM=`echo $i | awk '{\
 gsub ("0","๐");\
 gsub ("1","๑");\
 gsub ("2","๒");\
 gsub ("3","๓");\
 gsub ("4","๔");\
 gsub ("5","๕");\
 gsub ("6","๖");\
 gsub ("7","๗");\
 gsub ("8","๘");\
 gsub ("9","๙");\
 print }'`
    sed -e "s/$MARKER/$TNUM/g" $FILE > tmp.svg
    inkscape -e tmp$i.png tmp.svg
done
$ chmod 755 runcard

เวลาใช้งานก็สั่งรันชื่อไฟล์ runcard เฉย ๆ จะได้ไฟล์ tmp1.png จนถึง tmp10.png ก็สามารถนำไฟล์เหล่านี้ไปพิมพ์งานได้ตามต้องการ

หมายเหตุ
ไม่แน่ใจว่า การใช้คำสั่ง awk ตามตัวอย่าง จะสามารถเขียนได้กระชับกว่านี้หรือไม่ รบกวนผู้รู้ช่วยแนะนำด้วยครับ

bash: Rip audio disc to mp3

มีงานต้องเก็บแผ่นซีดีธรรมะลงไว้ในฮาร์ดดิสก์เป็นจำนวนมาก เพื่อให้สะดวก จึงทำเป็นสคริปต์ให้ใช้งานได้สะดวก

ลักษณะของโปรแกรมคือ เมื่อเราใส่แผ่นแล้วสั่งรันโปรแกรม เขาจะถอดไฟล์เสียงทั้งหมดมาเป็นไฟล์ mp3 ที่ไดเรกทอรีปัจจุบัน โดยเอาข้อมูลชื่อเพลงจากแผ่นมาเป็นชื่อไฟล์

ก่อนอื่นติดตั้งแพกเกจที่จำเป็นก่อน

$ sudo aptitude install lame cdda2wav

สคริปต์มีดังนี้

$ sudo vi /usr/local/bin/d.audio2mp3
#!/bin/bash
# Rip audio disc to mp3
# 
# USAGE: $0 prefix
# exam1: $0 T2
#    -> T2-01-Title1.mp3
#       T2-02-Title2.mp3
#       ...
# in current dir
# 
# Requist: aptitude install lame cdda2wav

if [ $1 ]; then PREFIX="$1-"; fi

DEV='/dev/cdrom'
TMP="/tmp/$USER/`basename $0`_$RANDOM"

mkdir -p $TMP
pushd $TMP

#to wave
cdda2wav -L 1 -D $DEV -B

#to mp3
for i in *wav; do
    NUM=`echo ${i%.*} | cut -d_ -f 2`-
    TITLE=`grep 'Tracktitle=' ${i%.*}.inf | cut -d\'  -f2`
    lame -h -V 2 $i $PREFIX$NUM$TITLE.mp3
done

popd
mv $TMP/*mp3 .

rm -rf $TMP

ทำให้รันได้

$ sudo chmod 777 /usr/local/bin/d.audio2mp3

เสร็จแล้ว

การใช้งานคือ ใส่แผ่น audio แล้วสั่งรัน d.audio2mp3 จะได้ไฟล์ mp3 มาอยู่ในไดเรกทอรีปัจจุบัน

การทำงานของสคริปต์คือ

  • แปลงข้อมูลเสียงมาเป็นไฟล์ wav ด้วยโปรแกรม cdda2wav โดยนำไปใส่ในไดเรกทอรีชั่วคราวใน /tmp (เพื่อป้องกันผู้ใช้หลายคน จึงใส่ตัวแปร $USER ไว้ด้วย เพราะโค๊ดต้นฉบับ เขียนบนเซิร์ฟเวอร์ที่มีผู้ใช้หลายคน)
  • แปลงไฟล์ wav ที่ได้มาเป็น mp3 โดยใช้โปรแกรม lame (ซึ่งอาจมีปัญหาว่าตอนนี้หา lame บน lenny กับ sid ไม่ได้แล้ว ให้ลองดูวิธีปรุงขึ้นมาเองได้ที่ วิธีปรุง lame โดยคุณโดม http://thisk.org/dr/node/32)
    โดยเอาข้อมูล Tracktitle จากไฟล์นามสกุล inf มาเป็นชื่อไฟล์ mp3 ที่ได้ รวมถึงใส่คำนำหน้า (จากตัวแปร $PREFIX) ให้ด้วย