code
stringlengths
1
1.96M
language
stringclasses
1 value
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: ctrlflow.sh # Description: # 1. if statement # if (expression1) { # action1 # } else if (expression2) { # action2 # } else { # action3 # } # 2. while statement # while (expression...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: comparision.sh # Description: # syntax: # expression { actions; } # (expr1) && (expr2) # (expr2) || (expr2) # Edit History: # 2010-12-23 File created. #==========================...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: syntax.sh # Description: # http://www.math.utah.edu/docs/info/gawk_1.html # http://www.grymoire.com/Unix/Awk.html#uh-4 # syntax: # awk 'script' files # # files: a list of one or mor...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: close.sh # Description: # # Edit History: # 2011-02-23 File created. #======================================================================== # pipe(|) to report, it will execute repor...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: redirect_pipe.sh # Description: # # Edit History: # 2011-02-23 File created. #======================================================================== #redirect ps | awk '{ print $1 > ...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: close.sh # Description: # # Edit History: # 2011-02-23 File created. #======================================================================== # pipe(|) to report, it will execute repor...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: redirect_pipe.sh # Description: # # Edit History: # 2011-02-23 File created. #======================================================================== #redirect ps | awk '{ print $1 > ...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: grep.sh # Description: # # Edit History: # 2011-01-14 File created. #======================================================================== #-i忽略大小寫,尋找有關unix的行數 #-n顯示行數 grep -in unix ...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: tr.sh # Description: # transliterate # # Class Description # alnum Letters and digits # alpha Letters # blank Horizontal whitespace # cntrl Control characters # digi...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: sort.sh # Description: # # Edit History: # 2011-01-14 File created. #======================================================================== tr '!?":;\[\]{}(),.\t\n' ' ' < ch15.doc | ...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: head.sh # Description: # # Edit History: # 2011-01-14 File created. #======================================================================== #the -ut (sort by last accessed time) #列出最...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: uniq.sh # Description: # # Edit History: # 2011-01-14 File created. #======================================================================== uniq fruit.txt #計算重複次數 uniq -c fruit.txt ...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: tail.sh # Description: # # Edit History: # 2011-01-14 File created. #======================================================================== #follow mode,可以觀察目前一直被寫入檔案的內容 tail -f acce...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: sort.sh # Description: # # Edit History: # 2011-01-14 File created. #======================================================================== tr '!?":;\[\]{}(),.\t\n' ' ' < ch15.doc | ...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: grep.sh # Description: # # Edit History: # 2011-01-14 File created. #======================================================================== #-i忽略大小寫,尋找有關unix的行數 #-n顯示行數 grep -in unix ...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: tr.sh # Description: # transliterate # # Class Description # alnum Letters and digits # alpha Letters # blank Horizontal whitespace # cntrl Control characters # digi...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: uniq.sh # Description: # # Edit History: # 2011-01-14 File created. #======================================================================== uniq fruit.txt #計算重複次數 uniq -c fruit.txt ...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: head.sh # Description: # # Edit History: # 2011-01-14 File created. #======================================================================== #the -ut (sort by last accessed time) #列出最...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: tail.sh # Description: # # Edit History: # 2011-01-14 File created. #======================================================================== #follow mode,可以觀察目前一直被寫入檔案的內容 tail -f acce...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: pretty-print.sh # Description: # Pretty-prints one of the ABS Guide author's favorite poems. # Edit History: # 2010-11-22 File created. #============================================...
Shell
#!/bin/sh # ftp_upload.sh - 使用ftp來上傳檔案 # show_help # 顯示求助檔的函數 show_help() { cat <<'EOF' usage: ftp_upload.sh [options...] ftp_host remote_dir files ... options: -u user set FTP login name. -p password set FTP login password. -P toggle passive mode. -h show this help message. ...
Shell
#!/bin/sh # ftp_upload.sh - 使用ftp來上傳檔案 # show_help # 顯示求助檔的函數 show_help() { cat <<'EOF' usage: ftp_upload.sh [options...] ftp_host remote_dir files ... options: -u user set FTP login name. -p password set FTP login password. -P toggle passive mode. -h show this help message. ...
Shell
#!/bin/sh # simple_command.sh - 基本的script(在一行內寫一個指令) date #顯示日期(只使用指令名稱) /usr/bin/uptime #顯示系統啟動之後經過的時(附路徑) uname -a #顯示OS的種類或版本等資訊(附選項)
Shell
#!/bin/sh # expect.sh - 用重新導向或expect來控制ftp的例子 # 使用ftp來取得ftp.kernel.org的檔案一覽表。 # 我們可以使用重新導向的方式,或是使用expect。 EMAIL='your@email.address.here' # 寫入郵件位址 # [使用重新導向的方法] # 依照原先預定的順序送出ftp的指令。 # 至於錯誤發生時該怎麼對應,則不列入考慮。 # 使用重新導向的方法中,無法順利對「Password:」做輸入 # 因此禁止ftp自動登入(「-n」選項)、 # 使用open和user指令來做登入 echo "open ftp.kernel.org use...
Shell
#!/bin/sh # expect.sh - 用重新導向或expect來控制ftp的例子 # 使用ftp來取得ftp.kernel.org的檔案一覽表。 # 我們可以使用重新導向的方式,或是使用expect。 EMAIL='your@email.address.here' # 寫入郵件位址 # [使用重新導向的方法] # 依照原先預定的順序送出ftp的指令。 # 至於錯誤發生時該怎麼對應,則不列入考慮。 # 使用重新導向的方法中,無法順利對「Password:」做輸入 # 因此禁止ftp自動登入(「-n」選項)、 # 使用open和user指令來做登入 echo "open ftp.kernel.org use...
Shell
#!/bin/sh # simple_command.sh - 基本的script(在一行內寫一個指令) date #顯示日期(只使用指令名稱) /usr/bin/uptime #顯示系統啟動之後經過的時(附路徑) uname -a #顯示OS的種類或版本等資訊(附選項)
Shell
#!/bin/sh # avinfo.sh - 取得聲音、影像檔的資訊 mplayer -identify -ao null -vo null -frames 0 "$1" 2>/dev/null | \ grep ^ID
Shell
#!/bin/sh # avinfo.sh - 取得聲音、影像檔的資訊 mplayer -identify -ao null -vo null -frames 0 "$1" 2>/dev/null | \ grep ^ID
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: xargs.sh # Description: # # Edit History: # 2011-01-19 File created. #======================================================================== #空白會造成xargs分成多個輸入 touch "All Space Ok" #每次...
Shell
#!/bin/sh # noecho.sh - 禁止輸入的echoback echo -n "Password: " #echo "Password: \c" # for Solaris stty -echo read password stty echo echo if [ "$password" = "secret" ]; then echo "Open sesami!" else echo "Bye!" fi
Shell
#!/bin/sh # getchar.sh - 輸入1個字的範例 echo "Hit any key!" #enable raw:輸入字元不用加enter, disable echo: 不會顯示輸入的字元 stty raw -echo #read 1 bytes from stdin char=`dd bs=1 count=1 2>/dev/null` stty -raw echo echo "Key pressed: '$char'"
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: heredocument.sh # Description: # # Edit History: # 2011-01-19 File created. #======================================================================== # 將地名一覽表排序過後再顯示 sort <<EOF Sapporo...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: space.sh # Description: # # Edit History: # 2011-04-30 File created. #======================================================================== # 包含空白字元或特殊符號的引數之使用例 # 含空白字元的例子 echo "MIL...
Shell
#!/bin/sh # inputbox.sh - 從純文字對話盒取得1行輸入 tmp=`mktemp /tmp/dialog.XXXXXXXX` || exit 1 dialog --inputbox "May I have your name, please?" 8 70 "Anonymous Coward" 2>"$tmp" dialog --msgbox "Your name is `cat "$tmp"`" 8 70 rm "$tmp"
Shell
#!/bin/sh # kcode.sh - 判別漢字碼,並顯示之 # 只讀入輸入檔的前10行。若輸入檔並未被指定的話 # 則從標準輸入讀取(/dev/fd/0) src=`head "${1:-/dev/fd/0}"` kcode="(unknown)" # 轉換成各種文字碼看看,若和原本的沒有變化 # 則表示其為正確的文字碼 for guess in "-w UTF-8" "-e EUC-JP" "-s Shift-JIS" "-j JIS"; do set -- $guess # $1=選項 $2=文字碼名稱 out=`echo "$src" | nkf $1` if [ "$src" = "$...
Shell
#!/bin/bash #============================================================================== # Copyright 2014 zod.yslin # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.or...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: nkf.sh # Description: # # Edit History: # 2011-04-30 File created. #======================================================================== # wget是從指定的URL下載檔案的指令 # (http://www.gnu.org/...
Shell
#!/bin/bash #============================================================================== # Copyright 2014 zod.yslin # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.or...
Shell
#!/bin/sh # kcode.sh - 判別漢字碼,並顯示之 # 只讀入輸入檔的前10行。若輸入檔並未被指定的話 # 則從標準輸入讀取(/dev/fd/0) src=`head "${1:-/dev/fd/0}"` kcode="(unknown)" # 轉換成各種文字碼看看,若和原本的沒有變化 # 則表示其為正確的文字碼 for guess in "-w UTF-8" "-e EUC-JP" "-s Shift-JIS" "-j JIS"; do set -- $guess # $1=選項 $2=文字碼名稱 out=`echo "$src" | nkf $1` if [ "$src" = "$...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: nkf.sh # Description: # # Edit History: # 2011-04-30 File created. #======================================================================== # wget是從指定的URL下載檔案的指令 # (http://www.gnu.org/...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: lsdiff.sh # Description: # # Edit History: # 2011-04-30 File created. #======================================================================== # lsdiff.sh - 「<(command)」的例子 # 若引數的數目不是...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: lsdiff.sh # Description: # # Edit History: # 2011-04-30 File created. #======================================================================== # lsdiff.sh - 「<(command)」的例子 # 若引數的數目不是...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: stderr.sh # Description: # # Edit History: # 2011-04-13 File created. #======================================================================== echo_error() { echo "ERROR:" $@ >&2 ; } p...
Shell
#!/bin/sh # yesno.sh - 用Yes/No詢問使用者 # yesno # 從標準輸入讀入一行資料,若回答是「yes/YES」的話,則為真(終止碼0) # 若為「no/NO」的話,則為假(終止碼1)。 yesno() { yn_result="" while [ -z "$yn_result" ]; do read yn_reply case "$yn_reply" in [Yy][Ee][Ss]) # yes, YES, Yes 等等... yn_result=0;; [Nn][Oo]) # no, NO, No 等等... yn_re...
Shell
#!/bin/sh # printf.sh - printf的範例 printf '1: <%5d> <%05d> <%-5d> \n' 123 123 123 printf '2: <%5.2f> <%05.2f> <%.3e>\n' 1.234 1.234 1.234 printf '3: <%s> <%-10s> <%.5s>\n' 'HELLO' 'HELLO' 'HELLO-WORLD' printf '4: miles\taway\n' printf '5: BEEP!\a\n' # 顯示目前進度很常使用的手法 i=0 while [ $i -le 10 ]; do printf '\rProcessing...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: redirect.sh # Description: # 標準輸入  (stdin) :代碼為 0 ,使用 < 或 << ; /dev/fd/0 # 標準輸出  (stdout):代碼為 1 ,使用 > 或 >> ; /dev/fd/1 # 標準錯誤輸出(stderr):代碼為 2 ,使用 2> 或 2>> ; # 垃圾桶黑洞裝置與特殊寫法 使用 2> /dev/null 或 ...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: variable_redirect.sh # Description: # The eval command is not used frequently in script writing. It is useful for those occasions where you want # to compose a shell command line using shell...
Shell
#!/bin/sh # yesno.sh - 用Yes/No詢問使用者 # yesno # 從標準輸入讀入一行資料,若回答是「yes/YES」的話,則為真(終止碼0) # 若為「no/NO」的話,則為假(終止碼1)。 yesno() { yn_result="" while [ -z "$yn_result" ]; do read yn_reply case "$yn_reply" in [Yy][Ee][Ss]) # yes, YES, Yes 等等... yn_result=0;; [Nn][Oo]) # no, NO, No 等等... yn_re...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: exec.sh # Description: # # Edit History: # 2010-12-20 File created. #========================================================================
Shell
#!/bin/sh # cecho.sh - 讓Solaris也可以使用「echo -n」 # 若使用「echo -n」時,會顯示「-n」的話,則重新定義echo成為一個函數 # 在其它的系統中則不做任何處理 if [ -n "`echo -n`" ]; then echo() { ( # 由於Solaris中無法使用local,所以在subshell中保護變數 if [ "$1" = "-n" ]; then shift postfix="\c" else postfix="" fi command echo "$*$postfix" # 執行原本的echo ...
Shell
#!/bin/sh # printf.sh - printf的範例 printf '1: <%5d> <%05d> <%-5d> \n' 123 123 123 printf '2: <%5.2f> <%05.2f> <%.3e>\n' 1.234 1.234 1.234 printf '3: <%s> <%-10s> <%.5s>\n' 'HELLO' 'HELLO' 'HELLO-WORLD' printf '4: miles\taway\n' printf '5: BEEP!\a\n' # 顯示目前進度很常使用的手法 i=0 while [ $i -le 10 ]; do printf '\rProcessing...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: read.sh # Description: # # Edit History: # 2011-05-04 File created. #======================================================================== #set separator as ':' to parse /etc/passwd,...
Shell
#!/bin/sh # getchar.sh - 輸入1個字的範例 echo "Hit any key!" #enable raw:輸入字元不用加enter, disable echo: 不會顯示輸入的字元 stty raw -echo #read 1 bytes from stdin char=`dd bs=1 count=1 2>/dev/null` stty -raw echo echo "Key pressed: '$char'"
Shell
#!/bin/sh # lock.sh - 使用鎖定檔(鎖定資料夾)的範例 LOCKDIR=/tmp/lock.sh.lockdir COUNTFILE=/tmp/lock.sh.count # count # 讀入共有檔案,在內容加1之後寫回去 count() { # 若引數有指定「lock」的話,則使用鎖定資料夾 # 來做互斥控制 if [ "$1" = "lock" ]; then # 在鎖定資料夾被製作成功之前,每隔一秒做同樣的動作 until mkdir $LOCKDIR 2>/dev/null; do sleep 1 done fi # 讀入共有檔案...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: space.sh # Description: # # Edit History: # 2011-04-30 File created. #======================================================================== # 包含空白字元或特殊符號的引數之使用例 # 含空白字元的例子 echo "MIL...
Shell
#!/bin/sh # echo_n - 不換行的echo指令 echo_n() { echo $ECHO_N "$*"$ECHO_C } # 檢查echo指令的相容性 if [ -z `echo -n` ]; then # Solaris以外的情況 ECHO_N="-n" ECHO_C="" else # Solaris的情況 ECHO_N="" ECHO_C="\c" fi # 畫面的寬度與高度 cols=`tput cols` lines=`tput lines` # 清除畫面 echo_n `tput clear` # 用各種屬性來寫字 c=`expr $co...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: mv.sh # Description: # # Edit History: # 2011-02-10 File created. #======================================================================== #檔案改名 #如何把目錄下檔名結尾為 .doc 的檔案改名為 .txt ? #可利用以下的...
Shell
#!/bin/sh # chk_interactive.sh - 調查是否為對話模式 # 若是對話模式的話,對使用者發出問題 if test -t 0; then echo "Are you sure? Press Ctrl-C to abort, or Enter to continue." read dummy fi # 實際的處理 echo "DO SOMETHING!"
Shell
#!/bin/sh # inputbox.sh - 從純文字對話盒取得1行輸入 tmp=`mktemp /tmp/dialog.XXXXXXXX` || exit 1 dialog --inputbox "May I have your name, please?" 8 70 "Anonymous Coward" 2>"$tmp" dialog --msgbox "Your name is `cat "$tmp"`" 8 70 rm "$tmp"
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: heredocument.sh # Description: # # Edit History: # 2011-01-19 File created. #======================================================================== # 將地名一覽表排序過後再顯示 sort <<EOF Sapporo...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: xargs.sh # Description: # # Edit History: # 2011-01-19 File created. #======================================================================== #空白會造成xargs分成多個輸入 touch "All Space Ok" #每次...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: stderr.sh # Description: # # Edit History: # 2011-04-13 File created. #======================================================================== echo_error() { echo "ERROR:" $@ >&2 ; } p...
Shell
#!/bin/sh # noecho.sh - 禁止輸入的echoback echo -n "Password: " #echo "Password: \c" # for Solaris stty -echo read password stty echo echo if [ "$password" = "secret" ]; then echo "Open sesami!" else echo "Bye!" fi
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: variable_redirect.sh # Description: # The eval command is not used frequently in script writing. It is useful for those occasions where you want # to compose a shell command line using shell...
Shell
#!/bin/sh # linenum.sh - 為檔案加上行號 # 若引數個數不正常的話,則顯示說明 if [ $# -ne 1 ]; then echo "usage: linenum.sh filename" exit 1 fi # 在IFS中指定換行文字 IFS=' ' ln=0 # 從標準輸入逐行讀入資料,與行號一同顯示 while read -r line; do # Solaris的read line ln=`expr $ln + 1` printf '%3d %s\n' "$ln" "$line" done < "$1" # 註:若read的「-r」選項不能使用的話, # ...
Shell
#!/bin/bash #=============================================================================== # Copyright 2011 zod.yslin # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.o...
Shell
#!/bin/sh # file_inode.sh - 調查檔案的i node編號 # 將ls的輸出分解成多個單字 set -- `ls -i -d "$1"` # 分解結果 # $1 - i node編號 # $2以後 - 檔案名稱 # 顯示i node編號 echo "$1"
Shell
#!/bin/bash #=============================================================================== # Copyright 2011 zod.yslin # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.o...
Shell
#!/bin/sh # file_datecmp.sh - 比較檔案的新舊 # 若內建的「test -e」不能使用時,則使用外部指令 if /bin/sh -c 'test -e "$0"' 2>/dev/null; then command="" # Solaris以外的情況 else command="command" # Solaris的情況 fi # 若指定的引數的數量不對的話,則顯示使用說明 if [ $# -ne 2 ]; then echo "usage: file_datecmp.sh file1 file2" exit 1 fi # 確定比較對象檔案存在 $...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: removeSpaceDir.sh # Description: # Shell Script to remove spaces in directory # Edit History: # 2011-02-16 File created. #================================================================...
Shell
#!/bin/bash #=============================================================================== # Copyright 2011 zod.yslin # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.o...
Shell
#!/bin/sh # tmpfile.sh - 製作暫存檔 # 製作暫存檔(若製作失敗的話則結束) tmpfile=`mktemp /tmp/scripttmp.XXXXXXXX` || exit 1 echo "hoge hoge" >>$tmpfile # 使用暫存檔的處理 rm $tmpfile # 使用結束之後刪除 # 在/tmp之外的地方製作暫存檔的例子 tmpfile=`mktemp "$HOME/tmp/scripttemp.XXXXXXXX"`|| exit 1 rm $tmpfile
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: removeSpaceFile.sh # Description: # # this shell script removes the spaces out of the name of every file argument # passed to it (handles an arbitrary number of files on the command line). # exa...
Shell
#!/bin/sh # dirsize.sh - 傳回資料夾的大小(磁碟使用量) # 若沒有指定引數的話,則顯示使用說明 if [ $# -ne 1 ]; then echo "usage: dirsize.sh dirname" exit 1 fi # 將「du -ks」的輸出結果用set分割成多個單字 set -- `du -ks "$1"` # 分解結果 # $1 - 大小(以KB為單位) # $2之後 - 資料夾名稱 # 顯示大小(以位元組為單位) echo $2 `expr $1 \* 1024`
Shell
#!/bin/sh # filesize.sh - 傳回檔案的大小 # 若引數沒有被指定的話,則顯示使用說明 if [ $# -ne 1 ]; then echo "usage: filesize.sh filename" exit 1 fi # 將「ls -l」的輸出用set分割為多個單字 set -- `ls -l "$1"` # 分解的結果 # $1 - 存取權 # $2 - 硬體連結數 # $3 - 擁有者使用者 # $4 - 擁有者群組 # $5 - 大小 # $6 - 最終更新日期的「月」 # $7 - 最終更新日期的「日」 # $8 - 最終更新日期的「年」或是「時:分」 # $9以後 - 檔名...
Shell
#!/bin/sh # rename_sed.sh - 使用正規表現變更檔名 # 若沒有引數的話,則顯示使用說明 if [ $# -lt 3 ]; then echo "usage: rename_sed.sh regexp replace files..." exit 1 fi # 設定正規表現與取代字串 regexp="$1" pattern="$2" shift 2 # 對其它的引數指定的檔案做重複處理 for orig do new=`echo "$orig" | sed s/"$regexp"/"$pattern"/` echo "$orig -> $new" mv "$or...
Shell
#!/bin/sh # dir.sh - 顯示指定的資料夾內的檔案一覽 dir="$1" # 指定的資料夾內的檔案一覽會被設定到$1, $2... 中 set -- "$dir"/* # 顯示資訊 echo "Directory: $dir" echo "Number of files: $#" echo "List of files:" # 對$1,$2...(檔名一覽)做重複處理 for name do echo " $name" done
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: rename.sh # Description: # Linux Rename Multiple Files At a Shell Prompt # Edit History: # 2011-03-28 File created. #=====================================================================...
Shell
#!/bin/sh # rmext.sh - 從檔名去除副檔名 # 使用expr的處理 rmext_expr() { bname=`expr "$1" : '\(.*\)\.'` [ $? -eq 0 ] && echo "$bname" || echo "$1" } # 使用sed的處理 rmext_sed() { echo "$1" | sed 's/\.[^.]*$//' } # 選擇expr版本或sed版本 rmext_expr "$1" #rmext_sed "$1"
Shell
#!/bin/sh # fileperm.sh - 調查檔案的存取權 [ -r "$1" ] && rp="readable" || rp="not readable" [ -w "$1" ] && wp="writable" || wp="not writable" [ -x "$1" ] && xp="executable" || xp="not executable" echo "$1 is $rp, $wp and $xp."
Shell
#!/bin/sh # linenum.sh - 為檔案加上行號 # 若引數個數不正常的話,則顯示說明 if [ $# -ne 1 ]; then echo "usage: linenum.sh filename" exit 1 fi # 在IFS中指定換行文字 IFS=' ' ln=0 # 從標準輸入逐行讀入資料,與行號一同顯示 while read -r line; do # Solaris的read line ln=`expr $ln + 1` printf '%3d %s\n' "$ln" "$line" done < "$1" # 註:若read的「-r」選項不能使用的話, # ...
Shell
#!/bin/sh # cmddir.sh - 顯示存放著引數所指定的指令的資料夾 fullpath=`which "$1"` # 取得指令的完整路徑 [ $? -ne 0 ] && exit 1 # 若找不到此指令,則結束 dirname "$fullpath"
Shell
#!/bin/sh # rename_sed.sh - 使用正規表現變更檔名 # 若沒有引數的話,則顯示使用說明 if [ $# -lt 3 ]; then echo "usage: rename_sed.sh regexp replace files..." exit 1 fi # 設定正規表現與取代字串 regexp="$1" pattern="$2" shift 2 # 對其它的引數指定的檔案做重複處理 for orig do new=`echo "$orig" | sed s/"$regexp"/"$pattern"/` echo "$orig -> $new" mv "$or...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: sha1_show.sh # Description: # Calculate sha1. # Edit History: # 2011-02-10 File created. #======================================================================== echo ""> sha1all; for f...
Shell
#!/bin/sh # dir.sh - 顯示指定的資料夾內的檔案一覽 dir="$1" # 指定的資料夾內的檔案一覽會被設定到$1, $2... 中 set -- "$dir"/* # 顯示資訊 echo "Directory: $dir" echo "Number of files: $#" echo "List of files:" # 對$1,$2...(檔名一覽)做重複處理 for name do echo " $name" done
Shell
#!/bin/sh # abspath2rel.sh - 將絕對路徑轉成相對路徑 # 若$1中是空值(也就是沒有任何的引數)的話,則顯示使用說明 if [ -z "$1" ]; then echo "usage: abspath2rel.sh abs_path" exit 1 fi # 檢查引數是否從「/」開始(是否為絕對路徑)。 # 若在expr的引數中指定「/」一個字的話,則會被看成是除法符號而發生錯誤, # 所以在前面加上一個多餘的字「x」。 if [ `expr x"$1" : x'/'` -eq 0 ]; then # 因為引數是相對路徑,所以發生錯誤 echo "$1: not an...
Shell
#!/bin/sh # csv2html.sh - 將CSV檔案轉成HTML的表 # 調查是否可以使用read -r if echo | read -r 2>/dev/null; then READ_R="-r" else READ_R="" fi # 若引數的數量不正確的話,則顯示使用說明 if [ $# -ne 1 ]; then echo "usage: cvs2html.sh csvfile" exit 1 fi # 在變數LF代入換行文字 LF=' ' echo '<TABLE>' # 只有在執行read指令的時候,IFS=換行文字 while IFS="$LF" read $R...
Shell
#!/bin/sh # trash.sh - 將指定的檔案移到垃圾桶($HOME/.Trash) # 若欲刪除的檔案不存在的話,則顯示使用方法之後,結束程式 if [ $# -eq 0 ]; then echo "usage: trash files_to_trash..." exit 1 fi # 若內建的「test -e」不能使用的話,則使用外部指令 if /bin/sh -c 'test -e "$0"' 2>/dev/null; then command="" else command="command" # Solaris的情況 fi # 若$HOME/.Trash不存在的話,...
Shell
#!/bin/sh # ftime_std.sh - 調查檔案的更新日期(標準篇) # 將ls的輸出分割為多個單字 set -- `ls -l -d "$1"` # 分解結果 # $1 - 存取權 # $2 - 硬體連結數 # $3 - 所有者使用者 # $4 - 所有者群組 # $5 - 大小 # $6 - 最終更新日期的「月」 # $7 - 最終更新日期的「日」 # $8 - 最終更新日期的「年」或是「時:分」 # $9之後 - 檔名 # 更新月份和更新日期永遠都是存在的 month=`printf '%02d' $6` # 01-12 day=`printf '%02d' $7` # 01-31 # 更新年份...
Shell
#!/bin/sh # rename_numalpha.sh - 將檔名中的一位數的數字換成英文字 # (1=A, 2=B, ..., 9=I) # 若沒有引數的話,則顯示使用說明 if [ $# -eq 0 ]; then echo "usage: rename_numalpha.sh files..." exit 1 fi # 對引數所指定的所有檔名做同樣的處理 for orig do new=`echo "$orig" | ruby -pe 'gsub!(/[1-9]/) { |m| (m.to_i+64).chr }'` echo "$orig ...
Shell
#!/bin/bash #======================================================================== # Author: YSLin # Email: # File Name: removeSpaceDir.sh # Description: # Shell Script to remove spaces in directory # Edit History: # 2011-02-16 File created. #================================================================...
Shell
#!/bin/sh # fileowner.sh - 傳回檔案的擁有者 # 若被給定的引數數量不對的話,則顯示使用方法 if [ $# -ne 2 ]; then echo "usage: fileowner.sh -u|-g filename" exit 1 fi # 解析選項 case "$1" in "-u") show="user" ;; "-g") show="group" ;; *) echo "$1: unknown option" exit 1 esac # 將「ls -l」的輸出用set分割成多個單字 set -- `ls -l -d "$2"` # 分解...
Shell
#!/bin/sh # rmext.sh - 從檔名去除副檔名 # 使用expr的處理 rmext_expr() { bname=`expr "$1" : '\(.*\)\.'` [ $? -eq 0 ] && echo "$bname" || echo "$1" } # 使用sed的處理 rmext_sed() { echo "$1" | sed 's/\.[^.]*$//' } # 選擇expr版本或sed版本 rmext_expr "$1" #rmext_sed "$1"
Shell
#!/bin/sh # tmpfile.sh - 製作暫存檔 # 製作暫存檔(若製作失敗的話則結束) tmpfile=`mktemp /tmp/scripttmp.XXXXXXXX` || exit 1 echo "hoge hoge" >>$tmpfile # 使用暫存檔的處理 rm $tmpfile # 使用結束之後刪除 # 在/tmp之外的地方製作暫存檔的例子 tmpfile=`mktemp "$HOME/tmp/scripttemp.XXXXXXXX"`|| exit 1 rm $tmpfile
Shell
#!/bin/sh # file_datecmp.sh - 比較檔案的新舊 # 若內建的「test -e」不能使用時,則使用外部指令 if /bin/sh -c 'test -e "$0"' 2>/dev/null; then command="" # Solaris以外的情況 else command="command" # Solaris的情況 fi # 若指定的引數的數量不對的話,則顯示使用說明 if [ $# -ne 2 ]; then echo "usage: file_datecmp.sh file1 file2" exit 1 fi # 確定比較對象檔案存在 $...
Shell
#!/bin/sh # log_split.sh - 將logfile分割為適當大小,並壓縮之 lpf=100 # 每100行做一次分割 # 若檔名沒有被指定的話,顯示使用說明 if [ $# -ne 1 ]; then echo "usage: log_split.sh logfile" exit 1 fi # 設定對象檔案的名稱,和去掉資料夾名稱的名稱 logfile="$1" splitbase=`basename "$1"`. # 把檔案做分割,若分割成功的話則分別去做壓縮 if split -l $lpf "$logfile" "$splitbase"; then for split i...
Shell
#!/bin/sh # dirsize.sh - 傳回資料夾的大小(磁碟使用量) # 若沒有指定引數的話,則顯示使用說明 if [ $# -ne 1 ]; then echo "usage: dirsize.sh dirname" exit 1 fi # 將「du -ks」的輸出結果用set分割成多個單字 set -- `du -ks "$1"` # 分解結果 # $1 - 大小(以KB為單位) # $2之後 - 資料夾名稱 # 顯示大小(以位元組為單位) echo $2 `expr $1 \* 1024`
Shell
#!/bin/bash #=============================================================================== # Copyright 2011 zod.yslin # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.o...
Shell
#!/bin/bash #=============================================================================== # Copyright 2011 zod.yslin # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.o...
Shell
#!/bin/bash #=============================================================================== # Copyright 2011 zod.yslin # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.o...
Shell