Bashã®å¤æ°ãã©ã¡ã¼ã¿å±éã®ã¾ã¨ã
Bashã®ãã©ã¡ã¼ã¿å±éã¯ããã«å¿ãã¦ãã¾ãã®ã§ã¡ã¢ã
å¤æ°ã®æåæ°
${#parameter}ã§å¤æ°ã®æåæ°ã調ã¹ããã¨ãã§ãã¾ãã
name=/var/log/boot.log echo ${#name} 17
wcãawkã³ãã³ãã使ãæ¹æ³ãããã¾ããã¹ãã¼ãã§ã¯ããã¾ããã*1
$ n=`echo $name | wc -c`; echo $(($n-1)) 17 $ echo $name | awk '{ print length($0)}' 17
é åãåãããã«é åæ°ã調ã¹ããã¨ãã§ãã¾ãã
$ array=(a b c d e) $ echo ${array[0]} a $ echo ${#array[@]} 5
å¤æ°ãã©ã¡ã¼ã¿å±é
å¤æ°ãåãåºãã¾ãããã¡ã¤ã«åããã£ã¬ã¯ããªåãç°¡åã«åãåºããã¨ãã§ãã¾ãã
$ name=/var/log/boot.log $ echo ${name##*/} boot.log
è²ã ãªæ¹æ³ãããã¾ãã
ãã©ã¡ã¼ã¿å±é | å 容 |
---|---|
${parameter#word} | å é ããåæ¹æçä¸è´ããä½ç½®ã¾ã§åãé¤ãã¾ã |
${parameter##word} | å é ããåæ¹æé·ä¸è´ããä½ç½®ã¾ã§åãé¤ãã¾ã |
${parameter%word} | æ«å°¾ããå¾æ¹æçä¸è´ããä½ç½®ã¾ã§åãé¤ãã¾ã |
${parameter%%word} | æ«å°¾ããå¾æ¹æé·ä¸è´ããä½ç½®ã¾ã§åãé¤ãã¾ã |
å®è¡çµæããããã並ã¹ã¦ã¿ãã
$ name=/var/log/boot.log $ echo ${name#*/} var/log/boot.log $ echo ${name##*/} #=> ãã¡ã¤ã«åãåå¾ boot.log $ echo ${name%*/} /var/log/boot.log $ echo ${name%/*} #=> ãã£ã¬ã¯ããªåãåå¾ /var/log $ echo ${name%%/*} $ echo ${name%.*} #=> æ¡å¼µåãåé¤ /var/log/boot $ echo ${name##*.} #=> æ¡å¼µåãåå¾ log $ echo ${name##/var/} #=> å é ã®/var/ãåé¤ log/boot.log
ãã¬ã¼ã¹å±é
ãã¬ã¼ã¹ã使ãã¨ç縮表ç¾ãããã¨ãã§ãã¾ãã/bin/ls 㨠/bin/ln ã¯ãã®ããã«æ¸ããã¨ãã§ãã¾ãã
$ echo /bin/l{s,n} /bin/ls /bin/ln $ ls -l /bin/l{s,n} -rwxr-xr-x 1 root root 27276 5æ 25 2008 /bin/ln -rwxr-xr-x 1 root root 89464 5æ 25 2008 /bin/ls
ãã¬ã¼ã¹å±éã¯ãã¡ã¤ã«ã®åå¨ã¯å¿ è¦ããã¾ããã
$ echo /{s,}bin/{a,b,c,d,e} /sbin/a /sbin/b /sbin/c /sbin/d /sbin/e /bin/a /bin/b /bin/c /bin/d /bin/e
ã¢ã¹ã¿ãªã¹ã¯ãä»ããã¨ãã·ã§ã«ãå ã«ã¢ã¹ã¿ãªã¹ã¯ãå±éããããåå¨ãããã¡ã¤ã«ãã¿ã¤ãã¦ããã¾ãã
$ echo /{s,}bin/{a,gr*} /sbin/a /sbin/grub /sbin/grub-install /sbin/grub-md5-crypt /sbin/grub-terminfo /sbin/grubby /bin/a /bin/grep
å¼æ°ã2ã¤ä½¿ãã³ãã³ãã«ã¯ãµãã£ãã¯ã¹ãä»ãã¦ãã¬ã¼ã¹å±éããã¨é常ã«ä¾¿å©ã«ãªãã¾ãã
$ echo testing.rb{,.backup} testing.rb testing.rb.backup
ãã¬ã¼ã¹å±éãå©ç¨ãã¦æ¥ä»æ¯ã«ããã¯ã¢ãã
$ cp -p testing.rb{,.`date +%Y%m%d`} $ ls testing.rb{,.`date +%Y%m%d`} testing.rb testing.rb.20090211
ãã¬ã¼ã¹å±éãå©ç¨ãã¦ä¸æ¬ã§ãã¡ã¤ã«ããªãã¼ã
$ ls audit2allow autoconf automake automake-1.6 autoreconf authconfig autoheader automake-1.4 automake-1.7 autoscan authconfig-tui autom4te automake-1.5 automake-1.9 autoupdate $ for i in *; do mv $i{,.bak}; done $ ls audit2allow.bak autoconf.bak automake-1.4.bak automake-1.7.bak autoreconf.bak authconfig-tui.bak autoheader.bak automake-1.5.bak automake-1.9.bak autoscan.bak authconfig.bak autom4te.bak automake-1.6.bak automake.bak autoupdate.bak
*1:wcã¯æ¹è¡ã¾ã§ã«ã¦ã³ãããã®ã§-1ããå¿ è¦ãããã¾ã