èªåç¨ã®Linuxã³ãã³ãã»ã·ã§ã«ã³ãã³ãåå¿é²
ç´°ãããã©ã¡ã¢ãã¦ãããããã®ãåºã¦ããã®ã§ãgitã¨vimã«ç¶ãã¦Linuxã³ãã³ãã»ã·ã§ã«ã³ãã³ãã®åå¿é²ã§ãã追è¨ããããã®ãåºã¦ãããéæç·¨éãã¾ãã
ãã¡ã¤ã«åããã¤ãã³ã§å§ã¾ããã¡ã¤ã«ãæ¶ãããããªãã¼ã ããããã
$ rm ./-foo.txt
ãã®æ§ã«./
ããæ¸ãå§ãã¾ãã
$ rm -foo.txt
ããæ¸ãã¨ãªãã·ã§ã³æ±ãã«ãªã£ã¦ãã¡ã§ãã
ã²ã¨ã¤åã®ãã¹ã«ç§»åãã
$ cd ~ $ cd ../ $ cd -
ãã®å ´åãcd -
ã§èªåã®ãã¼ã ãã£ã¬ã¯ããªã«ç§»åã§ãã¾ããä»çµã¿ã¯ãããããã¾ãããããã¤ãã³ã§ä¸ã¤åã®ãã£ã¬ã¯ããªã«ç§»ååºæ¥ãããã§ãããã¤ãã³ã¯$OLDPWDã¨åãã ãããid:calpoãããããã¨ããããã¾ãã
$ ls --
ls
ã®å ´åã¯ls --
ã¨ãã¤ãã³2ã¤ã§ãã²ã¨ã¤åã®ãã£ã¬ã¯ããªå
ãè¦ãã¾ããã
ã¨ããã¦ã¼ã¶ã¼ã®ãã¼ã ãã£ã¬ã¯ããª
$ cd ~foo $ cd /home/foo
ãã®2ã¤ã¯åãåãããã¾ãã~
ãèªåã®ãã¼ã ãã£ã¬ã¯ããªãªã®ã¯ç¥ã£ã¦ã¾ãããããã®æ¸ãæ¹ã¯ç¥ãã¾ããã§ããã
å®è¡ä¸ã®ã³ãã³ããããã¯ã°ã©ã¦ã³ãã«ç§»è¡ãã
Ctrl+z ^Z [1]+ Stopped sudo port selfupdate $ bg 1 [1]+ sudo port selfupdate & $ jobs [1]+ Running sudo port selfupdate &
ãã®ä¾ã§ã¯MacPorts*1ã®selfupdate
ããä¹
ãã¶ãã®å®è¡ã§å²ã¨é·ãã£ãã®ã§ããã¯ã°ã©ã¦ã³ãã«æã£ã¦ãã£ã¦ã¿ã¾ããã
bg 1
ã®1
ã¯çç¥ããã¨ãç´è¿ã§æ¢ããã¸ã§ããæä½ãããã¨ã«ãªãã¾ãã
$ fg 1 sudo port selfupdate
ããã¯ã°ã©ã¦ã³ããããã©ã¢ã°ã©ã¦ã³ãã«æã£ã¦ãããå¦çä¸ã¯æ¨æºåºåã«ãã¾ãä½ãåºããªãã³ãã³ããå®è¡ä¸ã ã¨ããã©ã¢ã°ã©ã¦ã³ãã«ç§»è¡ãã¦ãããã°ããä½ãåºãªãã®ã§ã¡ãã£ã¨å¿é ã«ãªãã
ã¡ãªã¿ã«ãselfupdate
ã¯ä½ãã解åããã®ãé·ããããbzip2
ãCPUã使ã£ã¦ã¾ãããCtrl+z
ã§æ¢ããã¨CPU使ç¨çãä¸ãããbg
fg
ã§åéãããã¨ãã¾ãCPUãªã½ã¼ã¹ãé£ãå§ããã®ã§ãCtrl+z
ã¯ã»ãã¨ã«æ¢ãã¦ãã¿ããã
ããã¯ã°ã©ã¦ã³ãã®ã¸ã§ããkillãã
$ jobs [1]+ Running sudo port selfupdate & $ kill -15 %1
ã¸ã§ãçªå·ã¯%
ãä»ãã¦æå®ã§ãã¾ãã
$ %1 $ fg 1
ã¤ãã§ã«%1
ã§fg 1
ã¨åæ§ã«ãããã¯ã°ã©ã¦ã³ããããã©ã¢ã°ã©ã¦ã³ãã«ç§»è¡åºæ¥ã¾ãã
çµäºã¹ãã¼ã¿ã¹*2
ã³ãã³ãçµäºæã«ã¯ãçµäºã¹ãã¼ã¿ã¹(exit-status)ãã¨å¼ã°ãããã³ãã³ãã®æå¦ã表ãæ°å¤ãç¹æ®å¤æ° $? ã«èªåã§è¨å®ãããã åã³ãã³ãã«ããç°ãªãããä¸è¬çã«ã¯ã³ãã³ãæåæã«ã¯ã0ããã失ææã«ã¯ã1ããè¨å®ãããã
command echo $?â ç´åã«å®è¡ããã³ãã³ãã®æå¦ã¯ãç¹æ®å¤æ° $? ã«è¨å®ããã¦ããå¤ã§ç¢ºèªããã
終了ステータス | UNIX & Linux コマンド・シェルスクリプト リファレンス
çµäºã¹ãã¼ã¿ã¹ã£ã¦ã®ãããã¿ããã ãã©ãphpãperlã¨æå³ãéã§ã¡ãã£ã¨é¦´æããªãããã
ãã¤ãã³ã¯æ¨æºåºåãã表ããã¨ãå¤ã
ç¹ã«æ±ºã¾ãã¨ããããã§ã¯ãªãããã§ãããå¤ãã®ã³ãã³ããããã¤ãã³ï¼-ï¼ã²ã¨ã¤ãæ¨æºå ¥åºåã®ãã¨ã¨èªã¿æ¿ãã¦å¦çããã¦ããã¾ãã
【linux】コマンドの結果を標準出力に出す at softelメモ
UNIXæåã®ç¿æ £ã§ãã¤ãã³ã¯æ¨æºåºåã表ããã¨ãå¤ãããããã§ãããã¾ã§ã³ãã³ãããããã®å®è£ ãã ããªãã ã¨ãã
ã§ã¯wgetã§è©¦ãã¦ã¿ãã
wget -O index.html http://www.softel.co.jp/
wgetã®çµæã¯index.htmlã«ä¿åããããã§ã¯æ¨æºåºåã«åºåãã¦ããã¤ãã§wcã«æ¸¡ãã¦è¡æ°ãæ°ããã«ã¯ã
wget -O - http://www.softel.co.jp/ | wc
【linux】コマンドの結果を標準出力に出す at softelメモ
- Oãªãã·ã§ã³ã®ä¿åå ã«ãã¤ãã³ãæå®ã
lnã³ãã³ãã§ã·ã³ããªãã¯ãªã³ã¯ãä½ãéã®ãã¹
ln
ã³ãã³ãã®å¼æ°ã«ç¸å¯¾ãã¹ã使ãã¨ããããããln -s ã·ã³ããªãã¯ãªã³ã¯ãåºæºã«ããå®ãã¡ã¤ã«ã¸ã®ãã¹ ä½ãã·ã³ããªãã¯ãªã³ã¯ã®ãã¹
ã¨ããäºã«ãªããä¸å¿æ¸ãã¦ã¿ããã©å¤åä½è¨æ··ä¹±ããã
$ cd ~ $ ls usr/local/bin/ foo $ ls usr bar
ãã®æ§ã«ã~/usr/local/bin/foo
ã¨~/bar
ã¨ãããã¡ã¤ã«ãããã¨ãã¦ãããããã«ã·ã³ããªãã¯ãªã³ã¯ãä½ãå ´åã®ä¾ã
$ ln -s usr/local/bin/foo foo
å®ãã¡ã¤ã« | ~/usr/local/bin/foo |
ã·ã³ããªãã¯ãªã³ã¯ | ~/foo |
$ ln -s ../../../bar usr/local/bin/bar
å®ãã¡ã¤ã« | ~/bar |
ã·ã³ããªãã¯ãªã³ã¯ | ~/usr/local/bin/bar |
curlã§HTTP Headerã確èªãã
$ curl -I GET http://example.com/
HTTP Headerãæ¨æºåºåãããããã ããHTTP HEADã¡ã½ããã
$ curl -I -X GET http://example.com/
HTTP GETã¡ã½ãããæå®ããäºãã§ããã
httpãµã¼ãã¼ã¯ãHEADã«å¯¾ãGETã¨åãHTTP Headerãè¿ããªãã¨ãããªãã«ã¼ã«ã«ãªã£ã¦ãããã©ãä½ãäºæ
ããã£ã¦GETã¨HEADã§å¸°ã£ã¦ããHeaderãéããªãã¦äºããããªãããã§ã
$ curl -i -X POST http://example.com/
-I
ãªãã·ã§ã³ã ã¨ãHTTP Headerã ãã§Bodyãè¦ãããªãã®ã§-i
ãªãã·ã§ã³ã®æ¹ã便å©ããã
sedã³ãã³ãã®ä½¿ãæ¹
-e
ã¯çç¥å¯è½
$ sed -e 's/foo/bar/' $ sed 's/foo/bar/'
ããªãã¿ã¯å¤æ´å¯è½
$ sed 's|http://|https://|'
æ£è¦è¡¨ç¾ã§ãã
$ sed 's| HTTP/1\.[0-1]||'
ããªãã¿è²ã
$ sed 's|foo|bar|' # | $ sed 's=foo=bar=' # = $ sed 'sAfooAbarA' # A $ sed 's(foo(bar(' # (
使ãæ©ä¼ã¯ã»ã¼ç¡ãã¨æããã©ããã£ã¦ã¿ãã*3è±åã¨ããåºæ¥ãã
$ sed 's(foo)(bar)' # ãã¡
æ®å¿µãªããPerlã®ããã«æ¬å¼§ãæ¬å¼§çãªããªãã¿ã¨ãã¦ä½¿ãäºã¯åºæ¥ãªãã¿ããã
è¡ãçãã
ä¾)
ï¼è¡ç®ã ããç½®æãããå ´å
$ sed '1s/linux/ãªããã¯ã¹/g' ãã¡ã¤ã«å > åºåå ãã¡ã¤ã«åï¼ãï¼ï¼è¡ç®ã ããç½®æãããå ´å
$ sed '1,10s/linux/ãªããã¯ã¹/g' ãã¡ã¤ã«å > åºåå ãã¡ã¤ã«åï¼è¡ç®ããæçµè¡ã¾ã§ãç½®æãããå ´å
$ sed '2,$s/linux/ãªããã¯ã¹/g' ãã¡ã¤ã«å > åºåå ãã¡ã¤ã«åï¼è¡ç®ããå é ãlã§ã¯ãã¾ãè¡ã¾ã§ãç½®æãããå ´å
$ sed '1,/^l/s/linux/ãªããã¯ã¹/g' ãã¡ã¤ã«å > åºåå ãã¡ã¤ã«åå é ãlã§ã¯ãã¾ãè¡ã ããç½®æãããå ´å
$ sed '/^l/s/linux/ãªããã¯ã¹/g' ãã¡ã¤ã«å > åºåå ãã¡ã¤ã«åå é ãlã§ã¯ãã¾ãè¡ä»¥å¤ãç½®æãããå ´å
http://www.nurs.or.jp/~sugi/sed.htm
$ sed '^t/!s/ãªããã¯ã¹/g' ãã¡ã¤ã«å > åºåå ãã¡ã¤ã«å
ãããªäºãåºæ¥ããããã
ä¸æ¬ç½®æ
$ find . -type f -print0 | xargs -0 sed -i -e 's/foo/bar/g' $ find . -type f -print0 | xargs -0 perl -pi -e 's/foo/bar/g' # $ find . -type f | xargs sed -i -e 's/foo/bar/g' # $ find . -type f | xargs perl -pi -e 's/foo/bar/g'
-print0
ã-0
ã«ã¤ãã¦ã¯Macでfindとxargsをパイプで繋いで使うなら -print0 と -0 を必ず使うメモ - kanonjiの日記ãåç
§ã
findã§é¤å¤
$ find . ! -name "*.svn*" -a -name "*.html"
.svnãã©ã«ããé¤å¤ and *.htmlããã ããå³å¯ã«.svnãã©ã«ãã«çµãã¦ãããã¯çåãfoo.svn.htmlãªãã¦ãã¡ã¤ã«ããã£ããæ··ããããã
$ find . ! -path "*.svn*" -a -name "*.html"
æ¡ã®å®æ¼ãã¦ãã®ã§æ¹ãã¦èª¿ã¹ã¦-path
ãè¦ã¤ãã¾ããã
cutã³ãã³ãã§ç©ºç½ãããªãã¿ã«ãã
$ cut -d' ' -f1,3 foo.log
GNU Screenä¸ã«ç»é¢ãåºã¾ã£ãã復帰ã§ããå ´åããã
C-a C-q
ããã¯ãå
ã
C-a C-s
ã§ç»é¢ãåºå®ã§ããããç¥ããªããã¡ã«ãã£ã¦ãã¾ã£ãå ´åã«ã復帰ããããã®C-a C-q
ã試ãã¦ã¿ãã¨ããã ãã®äºã
ASCIIå¶å¾¡ã³ã¼ã | ä¿¡å· | GNU Screenã§å ¥å | Terminalããå ¥å |
DC1 | XON | C-a C-q |
C-q |
DC3 | XOFF | C-a C-s |
C-s |
å¥ã«GNU Screenã®æ©è½ã¨ãã訳ã§ã¯ãªããASCIIå¶å¾¡ã³ã¼ãã®DC1, DC3ã«å¯¾ãã¦ããããåãããããä¾ãã°top
ã³ãã³ããåããã¦ãéã«C-s
ãå
¥åããã¨ãç»é¢ãåºå®ããããããããç¶æ³ã§ãããªãã¨ãã訳ãããªãã¨æããã©ããã®è¾ºã¾ã ã¡ããã¨ææ¡ã§ãã¦ãªãã
C-a
ã¯GNU Screenã®ã³ãã³ãæåã
ã³ãã³ããã°ã«ã¼ãã³ã°ãã
$ { cat 20120601.log; cat 20120602.log; } | grep foo
æ³¢æ¬å¼§ã§ã³ãã³ãã®ã°ã«ã¼ãã³ã°ãåºæ¥ãã
{ cat 20120601.log; cat 20120602.log; } #OK { cat 20120601.log;cat 20120602.log;} #OK {cat 20120601.log; cat 20120602.log; } #NG
ä½æ
ãå·¦ã®æ³¢æ¬å¼§ã¨ã³ãã³ãã®éã®ã¹ãã¼ã¹ã¯ãç¡ããã¨ã¨ã©ã¼ã«ãªãã¾ãããã¨æå¾ã®ã³ãã³ãã®å¾ãã®;
*4ãå¿
é ã
make -n installã§dry run
$ man make [snip] -n, --just-print, --dry-run, --recon Print the commands that would be executed, but do not execute them. [snip]
dry runã§installæã«ã©ãã«ãã¡ã¤ã«ãé ç½®ãããããã¤ã³ã¹ãã¼ã«åã«ç¢ºèªã§ããã
curlã§ãªã¸ã¥ã¼ã
$ curl -C - -o foo.tar.gz httpï¼//www.example.com/foo.tar.gz ** Resuming transfer from byte position 772428440 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 77.4M 100 77.4M 0 0 326k 0 0:04:03 0:04:03 --:--:-- 163k
ãããªæãã§ãªã¸ã¥ã¼ã ã§ãã¾ããä¸å®å®ãªåç·ã§ã¡ãã£ã¨ã§ãããã¡ã¤ã«ããã¦ã³ãã¼ãããªãã¨ãªããªãæãªããã«æ´»èºã
ãªã-C 10
ã ã¨ããã¡ã¤ã«ã®é ãã11byteãããªã¸ã¥ã¼ã ãããã
curlã§POST, PUT, DELETE
# GET $ curl http://localhost:3000/users.xml $ curl http://localhost:3000/users/3.xml # POST $ curl http://localhost:3000/users -X POST -d "user[name]=postman" -d "user[age]=19" # PUT $ curl http://localhost:3000/users/4 -X PUT -d "user[name]=putman" -d "user[age]=20" # DELETE $ curl http://localhost:3000/users/5 -X DELETEcurlコマンドからのRESTなリクエストを送り方 - ちくわプログラマにっき
curlã§Cookie
$ curl -c foo.txt httpï¼//www.example.com/ #Cookieä¿å $ curl -b foo.txt httpï¼//www.example.com/ #Cookieéä¿¡ $ curl -b "foo=bar; baz=qux;" httpï¼//www.example.com/ #Cookieéä¿¡
大ä½ãããªæãã
$ curl --cookie-jar foo.txt httpï¼//www.example.com/ #Cookieä¿å $ curl --cookie foo.txt httpï¼//www.example.com/ #Cookieéä¿¡
ä¸å¿ããªãã·ã§ã³ããã«ãã¼ã ã§æ¸ãã¨ãããªãã
git grepã®æ§ã«ãã©ã«ãå ãgrepãã
$ grep foo -r . $ find . -type f -print0 | xargs -0 grep foo
find xargs grep
ã§ãã£ã¦ããã©grep
ã®-r
ãªãã·ã§ã³ãããã便å©ã§ããfind xargs grep
ã®æ¹ã¯Macでfindとxargsをパイプで繋いで使うなら -print0 と -0 を必ず使うメモ - kanonjiの日記ã¨ããäºããã£ã¦ãã³ãã³ããé·ããªã£ã¦å²ã¨ããã©ãããã
ããããããã¡ã¤ã«åã®ãªã¹ããåå¾
$ grep foo -r . -l
åã«grep
ã®-l
ãªãã·ã§ã³ã£ã¦ã ãã ãã©ãå¿ããã¡ãªã®ã§ã¡ã¢ãã¦ããã¾ãã
ä¸é¨é¤å¤ãã
$ grep --exclude=.svn foo -r .
psã³ãã³ãã§ã¨ã«ããå ¨æ å ±ãåºåãã
$ ps aux
$ ps auxf #ããªã¼æ§é ã§è¡¨ç¤ºãMacã ã¨ä½¿ããªãã
a | å ¨ã¦ã¼ã¶ã¼ã®ããã»ã¹ã表示 |
u | ã¦ã¼ã¶ã¼åããã®ä»è©³ç´°ãªæ å ±ã表示 |
x | å¶å¾¡ç«¯æ«ã®ç¡ãããã»ã¹ã表示 |
grepã®ãã¤ã©ã¤ãããã¤ãããlessã§ããã¤ã©ã¤ããã
$ grep foo --color=always | less -R
--color=auto
ã§ãè¡ããã£ã½ãæ
å ±ãæã£ããã©ãèªåã®ç°å¢ã§ã¯--color=always
ãããªãã¨ã ãã ã£ãã
curlã§ajaxã¢ã¯ã»ã¹ãè£ ã
$ curl -H "X-Requested-With: XMLHttpRequest;" http://example.com
åã«ãããã¼ã«è¿½å ãã¦ããã ããªãã ãã©ãjQueryçã使ãã¨ããã®æ¡å¼µãããã¼ãã¤ããããã®ã§ãæåã§ã¤ããäºã§ajaxã¢ã¯ã»ã¹ãè£ ãããããã¼ã«è¿½å ãã以å¤ã¯æ®éã®ã¢ã¯ã»ã¹ãªã®ã§ãã ä»ããã ããªãã ãã©ã
Macã§ãã¼ãã使ã£ã¦ããããã»ã¹ã調ã¹ã
$ lsof -i :3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 16099 myuser 12u IPv4 0xffffff8026416c00 0t0 TCP *:mysql (LISTEN) MySQLWork 40355 myuser 22u IPv4 0xffffff80189f3160 0t0 TCP localhost:51146->localhost:mysql (CLOSED)
å¤åMacå°ç¨ã
ç°å¢å¤æ°ã¨ã·ã§ã«å¤æ°ãä¸è¦§
$ printenv #ç°å¢å¤æ°ã®ä¸è¦§ $ set #ã·ã§ã«å¤æ°ã®ä¸è¦§ $ printenv TERM #TERMã®å¤ã ã表示
HDDå ¨åãããã¡ã¤ã«ãfindãã
$ find / -name foo.log 2>/dev/null
Permission denied ã鬱é¶ããã®ã§ãæ¨æºã¨ã©ã¼ãæ¨ã¦ãã
大ä½ã©ã®ãã©ã«ãã容éé£ã£ã¦ãã®ã確èªãã
$ du -hxd 1
ããããããMac OS Xã®ãªãã·ã§ã³ä½ç³»ããï¼
ãã¡ã¤ã«ã®md5 sha1ããã·ã¥å¤ãæ±ãã
$ md5sum foo.txt $ sha1sum foo.txt $ sha1sum foo.txt bar.txt #è¤æ°æå® $ sha1sum *.txt #ã¯ã¤ã«ãã«ã¼ã # Mac OS X $ md5 foo.txt $ openssl sha1 foo.txt
Macã¯Linuxã¨ä½¿ããã³ãã³ããéãæ§åã
è¤æ°æå®ãã¯ã¤ã«ãã«ã¼ãã¯ããã¾ãã¾èª¿ã¹ãã¨ããã«æ¸ãã¦ãã£ãã®ã§sha1sum
以å¤ã§ã使ããããããªãã
2007å¹´ç¾å¨ãSHAã¯çæãããããé·ãç°ãªãSHA-1 (160ããã)ãSHA-224ãSHA-256ãSHA-384ãSHA-512ã®5種é¡ãåå¨ãã¦ããã
Secure Hash Algorithm - Wikipedia
ã°ã«ã¼ãã®ã¡ã³ãã¼ã§1993å¹´ã«ã¯ããã«çºè¡¨ããããã®ã¯ãå ¬å¼ã«ã¯SHAã¨å¼ã°ãã¦ãããããããã®å¾ã®ãã®ã¨åºå¥ããããã«ãã°ãã°SHA-0ã¨å¼ã°ãã¦ããã2å¹´å¾ãSHAã«åãã¦ã®å¾ç¶ã¨ãªãSHA-1ãçºè¡¨ããããããã«SHA-224ãSHA-256ãSHA-384ãSHA-512ã¨4ã¤ã®å¤å½¢ããå¢å ããåºåã®ç¯å²ã¨ããããªãã¶ã¤ã³ã®éãã§çºè¡ããã¦ããããããã¯ãã°ãã°ã¾ã¨ãã¦SHA-2ã¨ãããã¦ããã
Secure Hash Algorithm - Wikipedia
shaã£ã¦ååç´ããããã®ã§ãã¡ã¢ã
æ¨æºå ¥åãã³ãã³ãã«æ¸¡ã
$ echo 'foo' | example $ example <<_EOT_ foo bar _EOT_
example foo.txt
ã®æ§ã«ããã¡ã¤ã«ããªãã·ã§ã³ã«åãã³ãã³ããæãã¨ãã¦ããã¡ã¤ã«ãä½ããã«å
容ã ãã³ãã³ãã«æ¸¡ãã«ã¯ãæ¨æºå
¥åã使ããããã¾ããecho
ããã¤ãããã®ã¯ç´ãæãã¤ãããã©ãæ¹è¡ãå«ããªãããã¢ããã¥ã¡ã³ãããªãã¤ã¬ã¯ãããæ¹æ³ã使ããã
リダイレクト (CLI) - Wikipedia
ã¾ããããããæ
å ±ãWikipediaã§è¦ã¤ããã¨ã¯æã£ã¦ãªãã£ãã
ãã¢ããã¥ã¡ã³ãå ã®ã·ã§ã«å¤æ°ãå±éããªãæ§ã«ãã
#ã$ãã表示ãããã¨ãã¯ã\$ãã®ããã«ã¨ã¹ã±ã¼ããã cat <<_EOT_ ãã¢ããã¥ã¡ã³ãä¸ã§ã¯å¤æ°ã使ç¨ã§ãã¾ãã \$1 㯠$1 ã§ãã _EOT_ # çµäºæåãã¨ã¹ã±ã¼ãããã¨ãã¢ããã¥ã¡ã³ãä¸ã®å¤æ°ã¯å±éãããªã cat <<'_EOT_' ã·ã³ã°ã«ã¯ãªã¼ãã§çµäºæåãå²ãã¨å¤æ°ã¯ç¡è¦ããã¾ãã \$1 㯠$1 ã§ãã `echo "ã³ãã³ãç½®æãç¡è¦ããã¾ãã"` _EOT_ cat <<\_EOT_ ããã¯ã¹ã©ãã·ã¥ã§ãåæ§ã§ãã \$1 㯠$1 ã§ãã `echo "ã³ãã³ãç½®æãç¡è¦ããã¾ãã"` _EOT_入力と出力 | UNIX & Linux コマンド・シェルスクリプト リファレンス
ãã¢ããã¥ã¡ã³ãå
ã§ã¯ãã·ã§ã«å¤æ°ã使ãããã$
ããã®ã¾ã¾æåã¨ãã¦æ±ãããå ´åãä¸è¨2ã¤ã®æ¹æ³ã§åºæ¥ãã¨ã®äºã
gccã§ãã使ããªãã·ã§ã³ãããã
æ¨æºå ¥åã¨æ¨æºåºåãåæã«ãªãã¤ã¬ã¯ããã
wc -l < foo.log > /tmp/log-line-count
foo.logãæ¨æºå
¥åã«ãªãã¤ã¬ã¯ããã¦wc
ã«æ¸¡ãããã®çµæã®æ¨æºåºåã/tmp/log-line-count
ã«ãªãã¤ã¬ã¯ããã¦æ¸ãè¾¼ãã§ã¾ãã
æååå ã§ã³ãã³ãå®è¡
$ echo "foo`echo .`bar" $ echo "foo$(echo .)bar"
ã©ã¡ããfoo.bar
ã¨åºåã
$ echo 'foo`echo .`bar' # foo`echo .`bar $ echo 'foo$(echo .)bar' # foo$(echo .)bar
ã·ã³ã°ã«ã¯ã©ã¼ãã ã¨å®è¡ãããªãã
kshãbashã®å ´åãã$(ã³ãã³ã)ãï¼$ï¼åä¸ã®ä¸¸æ¬å¼§ï¼ã¯ããã¯ã¯ã©ã¼ãæå®ã`ã³ãã³ã`ãã¨åãã
http://www.ne.jp/asahi/hishidama/home/tech/unix/sh.html#backquote
$()ã使ããã·ã§ã«ãªããããã¯ã¯ã©ã¼ãããã$()ã使ãã¹ããï¼ããã¯ã¯ã©ã¼ãã¯å¤ãå½¢å¼ã§ããããã¹ãã§ããªãããéå§ã¨çµäºãã¾ãããããï¼
ããã¯ã¯ã©ã¼ãããã$()
ãæ¨å¥¨ãããã
echo $(cd $(dirname $0);pwd)
http://unoh.github.io/2008/09/02/bashtips.html
$()
ã§ã®ãã¹ãã®ä¾ã
é¢æ°ã®æç¡ã§åå²
$ type __git_ps1 > /dev/null 2>&1 && echo ok # okãåºå $ type __git_ps1_ > /dev/null 2>&1 && echo ok # åºåãããªã
Bashでコマンドの存在チェックはwhichよりhashの方が良いかも→いやtypeが最強 - Qiita
ã·ã§ã«ã¹ã¯ãªããã§ifæã®å ´å
if type __git_ps1 > /dev/null 2>&1 ; then # Do something. fi
æ¨æºåºåãæ¨æºã¨ã©ã¼ã¯æ¨ã¦ã¦ãçµäºã¹ãã¼ã¿ã¹(exit-status)ã§å¤å¥ãããã ã¨ãæãã
é¢æ°ã®æç¡ãããªããã©ãã¡ãã£ã¨ä¼¼ã¦ãäºã¨ããäºã§testã³ãã³ã使ã£ã¦cron
0 0 * * * /usr/bin/test $( date -d '+1 day' +\%d ) -eq 1 && /home/hoge/cron/foo.shâã§ãæ¯æææ«æ¥ã®é¶æã«foo.shãå®è¡ãããããã«ãªãã¾ãã
http://qiita.com/yadok/items/5c99cecdb17a436351f2
wcã®çµæãã空ç½ãåãé¤ã
$ cat file | wc -l | td -d ' '
wc
ã¯è¡æ°ã®åã«å¹¾ã¤ãã®ç©ºç½ãå
¥ãã®ã§ãå ´åã«ãã£ã¦ã¯éªéã«ãªãã¾ãããã®ç©ºç½ãtd
ã§åãé¤ãã¾ãã
å¤æ°ã«å ¥ããæ¹è¡ãåºåãã
$ FOO=$(ls -1) $ echo $FOO foo bar baz $ echo "$FOO" #ããã«ã¯ã©ã¼ãã§å²ãã¨æ¹è¡ãåºåããã foo bar baz
findãããã¡ã¤ã«ããã¤ãã§å¥ã³ãã³ãã«1åãã¤æ¸¡ããã
$ find . -type f -name *.csv | while read f; do some_command $f; done
パイプ出力を現在のシェル上のwhileに喰わせる上手いやり方 - Qiita
ã¡ãªã¿ã«ãå¥ã³ãã³ãã«æ¸¡ãããããªãã¦å¤æ°ã«å ¥ãããããªå ´åã¯ãwhileããµãã·ã§ã«ã§å®è¡ããã¦ãã¾ãããããwhileã®ä¸ã§å¤æ°ã«å ¥ãã¦ããæ¶ãã¦ãã¾ããããã§ãã
vmstatã«æ¥æãä»ãã¦ãã¡ã¤ã«ã«åºåãã
$ vmstat 1 | gawk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0 } { fflush() }' >> vmstat.log $ vmstat 1 | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0 } { system(":") }' >> vmstat.log
ãªããã¯ããããªãã®ã ãããã¡ã¤ã«ã¸ã®æ¸ãè¾¼ã¿ãããã¤ãå¾ãã®ããã»ã¹ï¼ä¸è¨ãªã awk ã grepï¼ãçµäºãã¦ããè¡ããã¦ããããã ããªã®ã§ã3ç§ããã«10ååºåããçµæã¯ãawk ãæ£å¸¸çµäºããå¾ã«ããã¡ã¤ã«ã¸æ¸ãè¾¼ã¾ãããéä¸ã§ vmstat ãå¼·å¶çµäºããå ´åã«ã¯ããã¡ã¤ã«ã¸ã¯ä½ãæ¸ãè¾¼ã¾ããªãã
[snip]
gawk ã§ãfflusu() é¢æ°ã使ãã°ãã¤ãããªãã¬ãã·ã¥ãããã¨ã®ãã¨ã§ã次ã®ããã«ããããã¾ããã£ãã
# vmstat -n 3 | gawk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 } { fflush() }' >> vmstat.log
awk ã§ããsystem() é¢æ°ã使ãã°ãã¤ãããªãã¬ãã·ã¥ãããã¨ã®ãã¨ã§ãï¼ã¡ãã£ã¨ãããªããï¼æ¬¡ã®ããã«ããããã¾ããã£ãã
# vmstat -n 3 | awk '{ print strftime("%Y/%m/%d %H:%M:%S"), $0 } { system(":") }' >> vmstat.log
(今さら) vmstat の結果に時間をつけてファイルに出力する - あしのあしあと