ã¾ãã¯äºã®çºç«¯ã説æãã¾ãã
/etc/passwdãã¡ã¤ã«ã¯ãã¦ã¼ã¶ã¼æ å ±ã1è¡ã«1ã¦ã¼ã¶ã¼åãã¤æ¸ãããããã¹ããã¡ã¤ã«ã§ãã1è¡ã¯ã³ãã³ã§åºåããã7ã¤ã®ãã£ã¼ã«ããããªãã¾ããåã¯ãæåã®ãã£ã¼ã«ãã§ããã¦ã¼ã¶ã¼åã¨ãæå¾ï¼7çªç®ï¼ã®ãã£ã¼ã«ãã§ãããã°ã¤ã³ã·ã§ã«ã ãã確èªãããã£ãã®ã§ããcat /etc/passwd ã¨ãã¦ãç´æ¥ç®ã§è¦ãã ã*1ã§ãã¦ã¼ã¶ã¼åã¨ãã°ã¤ã³ã·ã§ã«ãèªã¿åããã®ã§ãããã¡ãã£ã¨è¾ãããªãã¹ãè¦ããã表示ãããã®ã§ãã
ãã®èª²é¡ãçè¨åãã«ãã¦ãLinux/Unixã³ãã³ãã©ã¤ã³ã§ã¡ãã£ã¨ããäºãããããã®å°å³ãªã³ãã³ãéã¨ã·ã§ã«æ§æãç´¹ä»ãã¾ãããã¹ã¯ãªããè¨èªï¼ä¾ãã°perlï¼ã使ãã°ããããããããã¨ãããæè¦ã»ãææã¯ã´ã¢ããã¢ã ã¨æãã¾ãããä»åã¯èãè³æã¡ã¾ãããï¼ã¹ã¯ãªããè¨èªå¦çç³»ã§ã¯ãªãï¼ã³ãã³ãã¨ã·ã§ã«ã®ç¯å²å ã§ã®ãã£ã¨è¯ãæ¹æ³ã¯æ¯éã«ãæ示ãã ããã
ï¼[追è¨]id:Magicantããã«è²ã ãæ示ããã ãã¾ããããããã¨ããããã¾ãã[/追è¨]ï¼
話é¡ã«ããã³ãã³ãï¼
- cutã³ãã³ã
- trã³ãã³ã
- echoã³ãã³ã
- printfã³ãã³ã
話é¡ã«ããã·ã§ã«æ§æï¼
- è¡ã®ç¶ç¶
- ããã¯ã¯ã©ã¼ã
- ã¨ã¹ã±ã¼ãã¨ã¯ã©ã¼ãã£ã³ã°
- foræ
å 容ï¼
- cutã³ãã³ãã£ã¦ãã£ãã便å©
- cutã³ãã³ãã®åºåããªãã¿ãæå®ãã
- ã³ãã³ãã©ã¤ã³ããã¿ãæåãå ¥åããã®ãä¸è¦å´
- ã³ãã³ãã©ã¤ã³ã«ãããã¨ã¹ã±ã¼ãã¨ã¯ã©ã¼ãã£ã³ã°
- printfã³ãã³ãã使ã£ã¦ã¿ã
- printfã³ãã³ãã®ç¹°ãè¿ãå®è¡
cutã³ãã³ãã£ã¦ãã£ãã便å©
/etc/passwdã®ãããªãåºåãæåã§åºåããããã£ã¼ã«ããããªãã¬ã³ã¼ãï¼ããã¹ãè¡ã§ããï¼ãæä½ããã«ã¯cutã³ãã³ãã使ãã¾ããman cut ã¨ã http://www.linux.or.jp/JM/html/gnumaniak/man1/cut.1.html ãè¦ã¦ãã ãããä»å使ããªãã·ã§ã³ã¯æ¬¡ã®2ã¤ã§ãã
- -d DELIM ã¾ã㯠--delimiter=DELIM
- -f FIELD-LIST ã¾ã㯠--fields=FIELD-LIST
-d ã«ã»ãã³ãã³æåã:ãã-f ã«åãåºããã£ã¼ã«ãã®çªå·ãæå®ãã¾ãã-fã®æå®ã®ä»æ¹ã¯ï¼
$ cut -d: -f1 /etc/passwd # æåã®ãã£ã¼ã«ãã ãåãåºã $ cut -d: -f1,7 /etc/passwd # 1çªç®ã¨7çªç®ã®ãã£ã¼ã«ããåãåºã $ cut -d: -f5- /etc/passwd # 5çªç®ããå¾ã®ãã£ã¼ã«ããåãåºã $ cut -d: -f-3 /etc/passwd # 3çªç®ã¾ã§ã®ãã£ã¼ã«ããåãåºã $ cut -d: -f3-6 /etc/passwd # 3çªç®ãã6çªç®ã¾ã§ã®ãã£ã¼ã«ããåãåºã $ cut -d: -f3,4,5,6 /etc/passwd # ä¸ã¨åãï¼3çªç®ãã6çªç®ã¾ã§ï¼
cut -d: -f1,7 /etc/passwd ãå®éã«ãã£ã¦ã¿ãã¨ï¼
[hiyama@microapplications ~]$ cut -d: -f1,7 /etc/passwd | head -10 root:/bin/bash bin:/sbin/nologin daemon:/sbin/nologin adm:/sbin/nologin lp:/sbin/nologin sync:/bin/sync shutdown:/sbin/shutdown halt:/sbin/halt mail:/sbin/nologin news: [hiyama@microapplications ~]$
å¥ãªä¾ã¨ãã¦ã/bin/ls -l ã®åºåãããã¼ããã·ã§ã³ããªã¼ãã¼ããã¡ã¤ã«åã ããåãåºããã¨ã«ãã¾ãããã/bin/ls -l ã®åºåã¯ç©ºç½ï¼çªå·0x20ã®æåï¼ã§åºåããã9ã¤ã®ãã£ã¼ã«ããããªãã®ã§ã cut -d ' ' -f1,3,9 ã使ãã°ããããã§ãããæ¡æãã®ä½åã®ç©ºç½ããã£ã¦ãã¾ãããã¾ãããtr ã® -s ï¼--squeeze-repeatsï¼ãªãã·ã§ã³ã§éè¤ãã空ç½ãæ½°ãã¦ããã°OKã§ãããªããã³ãã³ãã©ã¤ã³è¡æ«ã«ã\ããä»ããã¨ãã©ããªå ´æã§ãã£ã¦ãè¡ãç¶ç¶ãã¦ã³ãã³ãã©ã¤ã³ãæ¸ãç¶ãããã¨ãã§ãã¾ãã
[hiyama@microapplications ~]$ /bin/ls -l /etc/vsftpd | tr -s ' ' \ > | cut -d ' ' -f1,3,9 åè¨ -rw-r--r-- root user_list.deny.sample -rw------- root vsftpd.conf -rw------- root vsftpd.conf.orig -rw------- root vsftpd.conf~ [hiyama@microapplications ~]$
æåã®è¡ã®ãåè¨ãã£ã¦ã®ãéªéã§ãããã¾ã¼ããã¨ãã¾ããããããã¨ããã¡ã¤ã«åã«ç©ºç½ãå ¥ãã¨å¤ãªãã¨ã«ãªãããã§ãããããã¾ã§ã¯ç¥ãã¾ãã*2ã
cutã³ãã³ãã®åºåããªãã¿ãæå®ãã
http://www.linux.or.jp/JM/html/gnumaniak/man1/cut.1.html ã®manãã¼ã¸ã«ã¯è¼ã£ã¦ãªãã®ã§ãããããã¦ãGNUã®cutå®è£
ã«ã¯ --output-delimiter ã¨ãããªãã·ã§ã³ããããåºåã®ããªãã¿ãå¤æ´ã§ãã¾ããï¼ä»¥ä¸ã®ããã«ãã³ãã³ãã©ã¤ã³ãå®çµãã¦ãªãã¨ãã¯ãè¡æ«ã®ã\ãããªãã¦ã次ã®è¡ã¸ã¨ã³ãã³ãã©ã¤ã³ãç¶ãããã¨ãã§ãã¾ããï¼
[hiyama@microapplications ~]$ cat /etc/passwd | > cut -s -d: -f1,7 --output-delimiter=' --> ' | head -10 root --> /bin/bash bin --> /sbin/nologin daemon --> /sbin/nologin adm --> /sbin/nologin lp --> /sbin/nologin sync --> /bin/sync shutdown --> /sbin/shutdown halt --> /sbin/halt mail --> /sbin/nologin news --> [hiyama@microapplications ~]$ /bin/ls -l /etc/vsftpd | > tr -s ' ' | cut -d ' ' -f1,3,9 --output-delimiter=' | ' åè¨ -rw-r--r-- | root | user_list.deny.sample -rw------- | root | vsftpd.conf -rw------- | root | vsftpd.conf.orig -rw------- | root | vsftpd.conf~ [hiyama@microapplications ~]$
--output-delimiter ãªãã·ã§ã³ãå©ç¨ããã¨ãè¦ããããæ¹åã§ãã¾ããã
ã·ã§ã«ã®è¤æ°è¡å ¥åã使ãã¨ãæ¹è¡ãåºåããªãã¿ã«ãããã¨ãã§ãã¾ãã以åããsedã«ãããæ¹è¡ã®è¡¨ç¾ã¨ã·ã§ã«ã®è¤æ°è¡å ¥åãã«ããã¦æ¬¡ã®ã³ãã³ãã©ã¤ã³ãç´¹ä»ãã¾ããã
$ echo $PATH | sed -e 's/:/\ > /g'
ããã¨åããã¨ããcutã³ãã³ãã使ã£ã¦ãã£ã¦ã¿ã¾ãã
[hiyama@microapplications ~]$ echo $PATH | cut -d: -f1- --output-delimiter=' > ' /usr/local/python/bin /usr/kerberos/bin /usr/local/bin /bin /usr/bin /usr/X11R6/bin /home/hiyama/bin /sbin /usr/sbin [hiyama@microapplications ~]$
ã³ãã³ãã©ã¤ã³ããã¿ãæåãå ¥åããã®ãä¸è¦å´
cat /etc/passwd | cut -s -d: -f1,7 ã§ã»ã¼å½åã®ç®çã¯éæãããã®ã§ãããåºååºåãè¨å·ãã¿ãã«ãããããè¦ãããã®ã§ã¯ãªããã¨æãã¾ãããcat /etc/passwd | cut -s -d: -f1,7 --output-delimiter='ï¼ã¿ãæåï¼' ã¨ããã°ããã¯ãã§ãã
ã¨ãããï¼ ãã¼ãã¼ãããã³ãã³ãã©ã¤ã³ã«ã¿ãæåãå ¥åãããã¨ãã§ããªããbashã¯ãã¿ãæåããã¡ã¤ã«åè£å®å½ä»¤ã¨è§£éãã¦ãã¼ã¿æåã¨ãã¦æ±ãã¾ãããããã¤ãã®ã¨ã¹ã±ã¼ãè¨æ³ããEmacsããã®é¡æ¨ã§ Ctrl+Q ãåç½®ãã¦ã¿ã¾ããããã¡ãã¦ã¼ã ãããããã
Googleã§æ¤ç´¢ãã¦ã¿ã¾ãããhttps://discussionsjapan.apple.com/message/100302527 ã«ãTerminal.app ã§ï¼bash ã«å¯¾ãã¦ãã¼ãã¼ãããã¿ãæåãå ¥åã§ãã¾ããï¼ã©ãããã°ããã®ã§ãããã?ãã¨ããåã®åé¡ã¨ä¸è´ãã質åãããã¾ãããããçããçå¤ãã®ãã³ãã³ã«ã³ã°ã£ãã§ã¾ã£ããã©ãéããªããä¾ãã°ï¼
èªåã¯ãã®æ¹é¢ã«ã¯è©³ãããªãã®ã§ãããããã°ãããã¨è§£æ±ºçãæä¾ãããã¨ã¯ã§ãã¾ãããããã®æ¹æ³ã¯ããã¯ãã§ãã
ãªããªãã ããã³ã¬(æ)ãï¼[追è¨]Magicantããã®ã³ã¡ã³ãã«ããã¨ãCtrl+V ãåç½®ããã°ããã¨ãããã¨ã§ãã[/追è¨]ï¼
ã§ãã¨ããããechoã³ãã³ãã§ã¿ãæåãåºåããããã¨ã«ãechoã³ãã³ãã® -e ãªãã·ã§ã³ã§ã¨ã¹ã±ã¼ããå¹ã*3ããã«ãªãã®ã§ããã使ãã¾ãã
[hiyama@microapplications ~]$ echo -n -e '\t' | od -x 0000000 0009 0000001 [hiyama@microapplications ~]$
確ãã«0x09çªã®æåï¼ã¿ããå¾ãããããã§ããããã¤ãã³ãã³ãã©ã¤ã³ã®ãªãã«åãè¾¼ãã«ã¯ããã¯ã¯ã©ã¼ãæ§æ `echo -n -e '\t'` ã使ãã°ããã§ãããã
[hiyama@microapplications ~]$ cat /etc/passwd | > cut -s -d: -f1,7 --output-delimiter="`echo -n -e '\t'`" | > head -10 root /bin/bash bin /sbin/nologin daemon /sbin/nologin adm /sbin/nologin lp /sbin/nologin sync /bin/sync shutdown /sbin/shutdown halt /sbin/halt mail /sbin/nologin news [hiyama@microapplications ~]$
ããããããã§ãä¸é¨åã¬ã¿ã¬ã¿ã§ãæ¡ãããã£ã¦ã¾ããã
ã³ãã³ãã©ã¤ã³ã«ãããã¨ã¹ã±ã¼ãã¨ã¯ã©ã¼ãã£ã³ã°
å ã«é²ãåã«ãã¿ããæ¹è¡ã®ã¨ã¹ã±ã¼ã表ç¾ã«ã¤ãã¦å°ã触ãã¦ããã¾ããã¾ãã¯å®é¨ããï¼
[hiyama@microapplications ~]$ echo push-button push-button [hiyama@microapplications ~]$ echo p\u\sh-\bu\tto\n push-button [hiyama@microapplications ~]$ echo "p\u\sh-\bu\tto\n" p\u\sh-\bu\tto\n [hiyama@microapplications ~]$ echo 'p\u\sh-\bu\tto\n' p\u\sh-\bu\tto\n [hiyama@microapplications ~]$
ããã¯ã¹ã©ãã·ã¥ï¼ç°å¢ã«ããåãã¼ã¯ï¼ï¼1æåã¯ãç¹å¥ãªè§£éãæã¤ãã¨ãããã¾ãã
- \u ãã®å¾ã«æåçªå·ãç¶ãã¦Unicodeã®1æå
- \s æ£è¦è¡¨ç¾å ã§ã¹ãã¼ã¹ãã¿ãã«ä¸è´
- \b ããã¯ã¹ãã¼ã¹ï¼0x08ï¼
- \t ã¿ãï¼0x09ï¼
- \n æ¹è¡ï¼0x0aï¼
ããããã·ã§ã«ã®ã³ãã³ãã©ã¤ã³ã§ã¯ãããã¯ã¹ã©ãã·ã¥ï¼1æåã¯åã«æåãã®ãã®ã表ãã ãã§ããã\nãã¯æåãnããªã®ã§ããããã«ã¯ã©ã¼ããã·ã³ã°ã«ã¯ã©ã¼ãã§å²ãã¨ãã\ããåãªãæåæ±ãã«ãªãã¾ããã\ããã¯ã©ã¼ããã©ã®ããã«ä½¿ããã¨ããã¨ãä¾ãã°ï¼
[hiyama@microapplications ~]$ echo \"double\ quoted\"\ \ \'single\ quoted\' "double quoted" 'single quoted' [hiyama@microapplications ~]$ echo "\"quoted\"" "quoted" [hiyama@microapplications ~]$ perl -e "print \"hello\\n\"" hello [hiyama@microapplications ~]$ echo '\"quoted\"' \"quoted\" [hiyama@microapplications ~]$ echo '"quoted"' "quoted" [hiyama@microapplications ~]$ echo '\' \ [hiyama@microapplications ~]$ echo '\'single\ quoted\'' > ' \single quoted' [hiyama@microapplications ~]$
æå¾ã®ä¾ããåããããã«ãã·ã³ã°ã«ã¯ã©ã¼ãå ã§ã·ã³ã°ã«ã¯ã©ã¼ããã¨ã¹ã±ã¼ããããã¨ã¯ã§ãã¾ãããã'\''ãï¼ã·ã³ã°ã«ã¯ã©ã¼ã - ããã¯ã¹ã©ãã·ã¥ - ã·ã³ã°ã«ã¯ã©ã¼ã - ã·ã³ã°ã«ã¯ã©ã¼ãï¼ã§ã¨ã¹ã±ã¼ãã§ããã£ã¦ããã·ãããã¾ãããããã¯ã¨ã¹ã±ã¼ããã¦ãããããªãã§ãã -- åã«ã¯ã©ã¼ããããæååãåå²ãã¦ããã ãã§ãã
ãã£ã¨ã¨ã°ãä¾ã¯ãã¨ã¹ã±ã¼ãç¥ããããã¯ã¹ã©ãã·ã¥ã®åµããã©ããã
printfã³ãã³ãã使ã£ã¦ã¿ã
æ¬ï¼ã«ã©ã ï¼ããããã«æ¡æããã¦åºåãã話ã«æ»ãã¾ããããæ°å¤ãæååã®ãã©ã¼ããã£ã³ã°ã¨ããã°ããã£ã±ãprintfã§ãããCè¨èªã®é¢æ°ã ããããªãã¦ãã³ãã³ãã¨ãã¦ãprintfãããã¾ãã
[hiyama@microapplications ~]$ printf [%5d]\\n 12 [ 12] [hiyama@microapplications ~]$ printf [%-5d]\\n 12 [12 ] [hiyama@microapplications ~]$ printf [%05d]\\n 12 [00012] [hiyama@microapplications ~]$ printf [%10s]\\n hello [ hello] [hiyama@microapplications ~]$ printf [%-10s]\\n hello [hello ] [hiyama@microapplications ~]$
å¤æ°ã¨ä¸ç·ã«ä½¿ããã¨ãã§ãã¾ãã
[hiyama@microapplications ~]$ x=12; printf [%05d]\\n $x [00012] [hiyama@microapplications ~]$ for x in 12 260 7 89; do printf [%05d]\\n $x; done [00012] [00260] [00007] [00089] [hiyama@microapplications ~]$
printfã³ãã³ãã®ç¹°ãè¿ãå®è¡
2ã¤ã®å¤æ°$userã¨$login_shellã«ãã¦ã¼ã¶ã¼åã¨ãã°ã¤ã³ã·ã§ã«ãé çªã«å ¥ã£ã¦ãããªããprintf %-10s%s\\n $user $login_shell ã¨ããã³ãã³ããç¹°ãè¿ãå®è¡ããã¨ããããã«æ¡æããããåºåãå¾ãããã¯ãã§ããã·ã§ã«ï¼bashï¼ã次ã®ãããªæ¯è¼çã¾ã¨ããªæ§æ(?)*4ããµãã¼ããã¦ãããã¨å¬ããã®ã§ããã
List=`cut -s -d: -f1,7 --output-delimiter=' ' /etc/passwd`;\ for user, login_shell in $List; # ãªã¹ããã2ã¤ãã¤é ç®ãåãåºãã¦2ã¤ã®å¤æ°ã«ãã¤ã³ããã do printf printf %-10s%s\\n $user $login_shell; done
ã©ãããforæã§ä½¿ããå¤æ°ã¯1ã¤ã ãã¿ããã§ãããxargs ã使ããããªãã¨ãæã£ãã®ã§ãããæ¨æºå ¥åãã2ã¤ãã¤ã¯ã¼ããåãåºãã¦ã³ãã³ãã«å¼æ°ã¨ãã¦æ¸¡ãæ¹æ³ããããã¾ãããï¼[追è¨]Magicantããã®ã³ã¡ã³ãã«ããã¨ãxargs -L 2 ã¨ããã°OKãã¾ããwhile 㨠read ã®çµã¿åããã§ã¯ã¼ãã2ã¤ãã¤åãåºããã¨ãã§ãããã¨ã[/追è¨]ï¼
ãããããªããããªãã¿ã¯ã³ãã³ã®ã¾ã¾ã®ãªã¹ããä½ã£ã¦ãprintfã®å¼æ°ã¨ãªãç´åã«ã³ãã³ã空ç½ã«ç½®ãæãããã¨ã«ãã¾ããã¾ããã¦ããå¿ã¾ãããããã¯ã¯ã©ã¼ãæ§æã®åºçªã§ãã
[hiyama@microapplications ~]$ List=`cut -s -d: -f1,7 /etc/passwd`;\ > for x in $List; > do > printf %-10s%s\\n `echo $x | tr : ' '`; > done | head -10 root /bin/bash bin /sbin/nologin daemon /sbin/nologin adm /sbin/nologin lp /sbin/nologin sync /bin/sync shutdown /sbin/shutdown halt /sbin/halt mail /sbin/nologin news [hiyama@microapplications ~]$
ããã§ç®çéæã§ããã³ãã³ãã©ã¤ã³ã1è¡ã«ããããªãï¼è¡ç¶ç¶ã使ã£ã¦æãè¿ãã¦ã¾ããï¼ï¼
[hiyama@microapplications ~]$ for x in `cut -s -d: -f1,7 /etc/passwd`;\ > do printf %-10s%s\\n `echo $x|tr : ' '`;done|head -10 root /bin/bash bin /sbin/nologin daemon /sbin/nologin adm /sbin/nologin lp /sbin/nologin sync /bin/sync shutdown /sbin/shutdown halt /sbin/halt mail /sbin/nologin news [hiyama@microapplications ~]$
ãããããã«ãã¦ããããã ããã·ã§ã«ã¯â¦
*1:æ®é㯠less ãä»ããã§ããããã
*2:åã¯ãã¡ã¤ã«åã«ç©ºç½ãå ¥ããªã主義ã§ãããããã空ç½ãå ¥ããã¡ã¤ã«ããã£ã¬ã¯ããªãé¿ãããã¨ã¯ã§ããªãã®ã§é£åãã¾ãã
*3:\nã\tã\bã\x20 ãªã©ã使ãã¾ãã
*4:foræã«è¤æ°ã®ã«ã¼ãå¤æ°ãæå®ããã¨ããã¹ãããããªã¹ããã¿ãã«ã®å å´ããå¤ãåãåºãä»æ§ãæ®éã§ããããããã·ã§ã«ã®ãªã¹ãï¼ç©ºç½åºåãã®ã¯ã¼ãåï¼ã¯ããã¹ãã§ããªãã®ã§ãä¸åº¦ã«è¤æ°ã®é ç®ãåãåºãã®ãããããã«æãã¾ãã