"Bash"의 두 판 사이의 차이

ph
이동: 둘러보기, 검색
7번째 줄: 7번째 줄:
 
*[http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html Bash Guide for Beginners]
 
*[http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html Bash Guide for Beginners]
 
*[http://www.linuxconfig.org/Bash_scripting_Tutorial Bash scripting Tutorial]
 
*[http://www.linuxconfig.org/Bash_scripting_Tutorial Bash scripting Tutorial]
{{break}}
+
 
  
 
=={{c|$SHLVL}}==
 
=={{c|$SHLVL}}==
20번째 줄: 20번째 줄:
 
  1
 
  1
 
http://stackoverflow.com/a/4511483/766330
 
http://stackoverflow.com/a/4511483/766330
{{break}}
+
 
  
 
==Regular expression match test==
 
==Regular expression match test==
26번째 줄: 26번째 줄:
 
  if <nowiki>[[ $gg =~ ^....grid.*$ ]]</nowiki> ; then echo $gg; fi
 
  if <nowiki>[[ $gg =~ ^....grid.*$ ]]</nowiki> ; then echo $gg; fi
 
[http://stackoverflow.com/a/2348495/766330]
 
[http://stackoverflow.com/a/2348495/766330]
{{break}}
+
 
  
 
=={{c|getopt}}==
 
=={{c|getopt}}==
47번째 줄: 47번째 줄:
 
[http://stackoverflow.com/a/33151456/766330]
 
[http://stackoverflow.com/a/33151456/766330]
  
{{break}}
+
 
  
 
=={{c|stty}}==
 
=={{c|stty}}==
53번째 줄: 53번째 줄:
 
  stty sane
 
  stty sane
 
이 외 다양한 옵션에 관해서는 [http://askubuntu.com/a/454663/400331 링크]참고
 
이 외 다양한 옵션에 관해서는 [http://askubuntu.com/a/454663/400331 링크]참고
{{break}}
+
 
  
 
==bash expansion : ranges==
 
==bash expansion : ranges==
120번째 줄: 120번째 줄:
 
  $ if true; then :; else echo; fi
 
  $ if true; then :; else echo; fi
 
[http://unix.stackexchange.com/a/134025/31363]
 
[http://unix.stackexchange.com/a/134025/31363]
{{break}}
+
 
  
 
==Default {{c|.bashrc}}==
 
==Default {{c|.bashrc}}==
 
  /etc/skel/.bashrc
 
  /etc/skel/.bashrc
 
[http://askubuntu.com/a/404428/400331]
 
[http://askubuntu.com/a/404428/400331]
{{break}}
+
 
  
 
==Tar the output on the fly==
 
==Tar the output on the fly==
133번째 줄: 133번째 줄:
 
  tar -c something | tar -C somefolder -xv
 
  tar -c something | tar -C somefolder -xv
 
[https://superuser.com/a/345396/108174]
 
[https://superuser.com/a/345396/108174]
{{break}}
+
 
 
==Useful {{c|date}} format==
 
==Useful {{c|date}} format==
 
  $ date +%D
 
  $ date +%D
143번째 줄: 143번째 줄:
 
  $ date +%T
 
  $ date +%T
 
  18:15:47
 
  18:15:47
{{break}}
+
 
  
 
==a==
 
==a==

2017년 4월 14일 (금) 19:08 판

manual, refs


$SHLVL

The variable tracks your shell nesting level

$ echo $SHLVL
1
$ bash
$ echo $SHLVL
2
$ exit
$ echo $SHLVL
1

http://stackoverflow.com/a/4511483/766330


Regular expression match test

Use double bracket and =~

if [[ $gg =~ ^....grid.*$ ]] ; then echo $gg; fi

[1]


getopt

long options

use getopt(not getopts) [2]

multiple arguments with one flag

깔끔한 방법이 없어보임. [3]

-o a1 -o a2 -o a3

이런식으로 계속 주는 수밖에 없는듯

default option getopt

$ getopt -s bash -o l:: -l list:: -- -ltest
 -l 'test' --
$ getopt -s bash -o l:: -l list:: -- --list foo
 --list  -- 'foo'
$ getopt -s bash -o l:: -l list:: -- --list=foo
 --list 'foo' --

::을 쓰면 되지만, optional argument를 명시할 때는 short option의 경우 option뒤에 바로 붙여야 하고 long option의 경우 =을 꼭 써주어야 한다. [4]


stty

터미널이 제정신이 아닌것 같다 싶으면 그냥

stty sane

이 외 다양한 옵션에 관해서는 링크참고


bash expansion : ranges

$ echo {d..h}
d e f g h

$ echo {d..Z}
d c b a _ ^ ]  [ Z

For more information, refer wiki.bash-hackers.org.

redirection

### cp file1 file2와 같다
$ cat < file1 > file2


string : $''

$ # Single quotation in single qoute
$ # http://stackoverflow.com/a/16605140/766330
$ echo \'sdlfkjsldf\' | sed $'s/\'//'
sdlfkjsldf'
$ #   $'string' : this is special.
$ #   below line won't work.
$ #   echo \'sdlfkjsldf\' | sed 's/\'//‘

[5]


or

$ # 'or' 
$ if [ -z "$1" ] || [ -z "$2" ]; then
>    echo "Usage: `basename $0` rslt_file target_folder"
> fi

ref. expressions used with if


extract tar archive from stdin

use - as the input file

cat largefile.tgz.aa largefile.tgz.ab | tar zxf -

[6]


find out what is using TCP port 80

# netstat -tulpn | grep :80

-p : You should be root

[7]


read from file or stdin

while read line 
do
  echo "$line"
done < "${1:-/dev/stdin}”

${1:-…} takes $1 if defined otherwise the file name of the standard input of the own process is used

[8]


no op

:

$ if true; then :; else echo; fi

[9]


Default .bashrc

/etc/skel/.bashrc

[10]


Tar the output on the fly

The same -f - option works for tarring as well.

tar -cf - something | tar -C somefolder -xvf -

GNU tar uses stdio by default:

tar -c something | tar -C somefolder -xv

[11]

Useful date format

$ date +%D
04/11/17
$ date +%F
2017-04-11
$ date +%R
18:15
$ date +%T
18:15:47


a