Bash
목차
- 1 manual, refs
- 2 $SHLVL
- 3 Regular expression match test
- 4 getopt
- 5 stty
- 6 bash expansion : ranges
- 7 redirection
- 8 string : $''
- 9 or
- 10 extract tar archive from stdin
- 11 find out what is using TCP port 80
- 12 read from file or stdin
- 13 no op
- 14 Default .bashrc
- 15 Tar the output on the fly
- 16 Useful date format
- 17 zip의 내용 보기
- 18 max numeric value
- 19 a
manual, refs
- Mendel Cooper - Advanced Bash-Scripting Guide (aka. ABS) (한글판)
- Bash Reference Manual
- bash features
- BASH Programming - Introduction HOW-TO ★
- Bash Guide for Beginners
- Bash scripting Tutorial
$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
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]
긴 옵션만 쓸 때 주의
\$ getopt -l test: -- --test abc 'abc' -- \$ getopt -o x -l test: -- --test abc --test 'abc' --
매뉴얼에 보면,
If no '-o' or '--options' option is found in the first part, the first parameter of the second part is used as the short options string.
결론: 헷갈리고 싶지 않으면 -o로 암거나 넣을것.
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/\'//‘
or
$ # 'or' $ if [ -z "$1" ] || [ -z "$2" ]; then > echo "Usage: `basename $0` rslt_file target_folder" > fi
extract tar archive from stdin
use -
as the input file
cat largefile.tgz.aa largefile.tgz.ab | tar zxf -
find out what is using TCP port 80
# netstat -tulpn | grep :80
-p : You should be root
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
no op
:
$ if true; then :; else echo; fi
Default .bashrc
/etc/skel/.bashrc
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
Useful date format
$ date +%D 04/11/17 $ date +%F 2017-04-11 $ date +%R 18:15 $ date +%T 18:15:47
zip의 내용 보기
less
명령으로 그냥 볼 수 있다.
아래 두 명령은 같다.
unzip -l zipfile less zipfile
https://superuser.com/a/216675
max numeric value
$ echo $((2**63 -1)) 9223372036854775807 $ echo $((2**63 )) -9223372036854775808
https://superuser.com/a/1030129/108174