檔案批次處理

#!/bin/bash

for file in ./*.mp3
do
    mpg -w ./wavs/"${file}".wav "$file"
done

grep 偵測字串是否存在

# 偵測看 port 22 在否?
testing=$(grep ":22 " ${testfile})

if [ "${testing}" != "" ]; then
    echo "SSH is running in your system."
fi

command param

    echo                                                                                                                                                                                      
    echo "Local Logging:"                                                                                                                                                                     
    echo "./dmcapture -l [-d device] [-f filter] [-o logfile]:"                                                                                                                               
    echo "      - Logs DM packets to \"logfile\" if specified, or to a script generated "                                                                                                     
    echo "        log file stored in the current working directory if not specified."                                                                                                         
    echo                                                                                                                                                                                      
    echo "Remote Logging:"                                                                                                                                                                    
    echo "./dmcapture [-d device] [-f filter] [-p netport] [-r rhost]"                                                                                                                        
    echo "      - Establishes a TCP connection with a remote machine \"rhost\" using port number "                                                                                            
    echo "        \"netport\", or a default port number of 23000 if not specified. DM packets are "                                                                                           
    echo "        exchanged over the TCP connection."                                                                                                                                         
    echo "      - Without the -r option, acts as a server application waiting for an incoming "                                                                                               
    echo "        connection request. Otherwise, acts as a client and attempts to establish a "                                                                                               
    echo "        connection with \"rhost\"."                                                                                                                                                 
    echo                                                                                                                                                                                      
    echo "OPTIONS:"                                                                                                                                                                           
    echo "-l            - Local logging if specified, remote logging otherwise"                                                                                                               
    echo "-a arch       - Specify architecture if not x86: arm, ppc, mips"                                                                                                                    
    echo "-d device     - ttyUSB port for DM logging"                                                                                                                                         
    echo "-f filter     - (Optional) DM filter to send to the device prior to logging"                                                                                                        
    echo "-p netport    - Remote logging TCP port (defaults to 23000)"                                                                                                                        
    echo "-r rhost      - (Optional) remote host to connect to"                                                                                                                               
    echo "-o logfile    - fully qualified DM log (output) file name"                                                                                                                          
    echo                                                                                                                                                                                      
}                                                                                                                                                                                             

while [ $# -gt 0 ]                                                                                                                                                                            
do                                                                                                                                                                                            
    case "$1" in                                                                                                                                                                              
    -a) arch=$2;;                                                                                                                                                                             
    -d) dev=$2;;                                                                                                                                                                              
    -f) filter=$2;;                                                                                                                                                                           
    -h) usage; exit;;                                                                                                                                                                         
    -l) ltype="local";;                                                                                                                                                                       
    -o) logfile=$2;;                                                                                                                                                                          
    -p) netport=$2;;                                                                                                                                                                          
    -r) rhost=$2;;                                                                                                                                                                            
    --help) usage; exit;                                                                                                                                                                      
    esac                                                                                                                                                                                      
    shift                                                                                                                                                                                     
done

getopts 引數有規則性

./shell -f param1 -g param2 -h param3

#!/bin/bash

while getopts "f:g:h:" flag; do
        case $flag in
f)      opt_f="$OPTARG";;
g)      opt_g="$OPTARG";;
h)      opt_h="$OPTARG";;
*)      opt_o="NO";;
        esac
done

echo "argument of -f is $opt_f"
echo "argument of -g is $opt_g"
echo "argument of -h is $opt_h"

shift $((OPTIND-1))
echo "other args: $*"

getopts & set 引數無規則性

./shell -a -b param1 -c param2 -d param3

#!/bin/bash

set - `getopt ab:c: $*`
while true
do
    case $1 in
    -a)     echo option -a
    shift
    ;;
    -b)     echo option -b=$2
    shift 2
    ;;
    -c)     echo option -c=$2
    shift 2
    ;;
    --)
    shift
    break
    ;;
    *)
    echo "error!"
    return 1
    ;;
    esac
done

變數的變數 variable name from variable

#!/bin/bash

A="abcd dcba"
B=A
C=${!B}
echo $C
#!/bin/bash
A=B
B="123"
eval var=\$$A
echo "var=$var" #var=123

results matching ""

    No results matching ""