Bash
목차
- 1 manual, refs
- 2 disable focus-in, focus-out event in terminal
- 3 json pretty print
- 4 parameter substitution
- 5 $SHLVL
- 6 Regular expression match test
- 7 getopt
- 8 stty
- 9 bash expansion : ranges
- 10 redirection
- 11 string : $''
- 12 or
- 13 extract tar archive from stdin
- 14 find out what is using TCP port 80
- 15 read from file or stdin (default argument)
- 16 default value syntax
- 17 emulating do while loop
- 18 no op
- 19 Default .bashrc
- 20 Tar the output on the fly
- 21 Useful date format
- 22 max numeric value
- 23 Get the name of the caller
- 24 Count occurrences of a character
- 25 head and tail of a file ★
- 26 To pipe stderr, and not stdout
- 27 nohup to already-running process
- 28 inspecting a text file
- 29 Array
- 30 Comparing strings
- 31 Regex: Match NOT curved or square bracket
- 32 substring
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
disable focus-in, focus-out event in terminal
printf "\e[?1004l"
json pretty print
use jq [3]
stdout으로 받으려면 필터를 넣어주어야 한다.
curl -s https://api.github.com/users/octocat/repos | jq '.' | cat
dot은 기본필터(아무것도 하지 않음) [4]
parameter substitution
<shl clsss=bash>ph 17:43:26 ~$ a=" xx yy " ~$ echo ${a// /}. xxyy. ~$ echo ${a/ /}. xx yy . ~$ echo ${a# }. xx yy . ~$ echo ${a## }. xx yy . ~$ echo ${a% }. xx yy . ~$ echo ${a%% }. xx yy . </shl> [5]
$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
if you want use a wildcard, do like this
[[ sed-4.2.2.tar.bz2 == *tar.bz2 ]] && echo matched
! is a negation. Like this,
[[ ! sed-4.2.2.tar.bz2 == *tar.bz2 ]]
getopt
long options
use getopt(not getopts) [8]
getopts가 builtin이다. getopt는 쓰지 않는것을 권장한(다는 얘기도 있고 아니라는 얘기도 있고 그렇)다. ref) getopt vs getopts
multiple arguments with one flag
깔끔한 방법이 없어보임. [9]
-o a1 -o a2 -o a3
이런식으로 계속 주는 수밖에 없는듯
simple example
http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt
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의 경우 =을 꼭 써주어야 한다. [10]
긴 옵션만 쓸 때 주의
$ 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로 암거나 넣을것.
About eval set --
getopt(s)다음에 eval set을 하는 이유 참고. Bash: Preserving Whitespace Using set and eval
set뒤에 오는것은 argument가 되고, eval은 말그대로 eval. 여기서는 tricky하게 쓰이는 것으로 side effect가깝다. set만 쓰면 아예 아무것도 하지 않은 것과 동일한 결과를 보여준다.
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 (default argument)
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
http://stackoverflow.com/a/7045517/766330
process substition을 쓴다. 이유는 여기의 pattern2에 관한 설명 참조
default value syntax
${1:-foo}
\$1이 unset이거나 null이면 foo가 된다.
${1-foo}
\$1으로 empty string(=null)이 가능하다. set 여부만 체크
https://stackoverflow.com/a/9333006/766330
emulating do while loop
Place the body of your loop after the while and before the test.
while check_if_file_present #do other stuff (( current_time <= cutoff )) do : done
http://stackoverflow.com/a/16491478/766330
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
max numeric value
$ echo $((2**63 -1)) 9223372036854775807 $ echo $((2**63 )) -9223372036854775808
https://superuser.com/a/1030129/108174
Get the name of the caller
The \$PPID variable holds the parent process ID. So you could parse the output from ps to get the command.
PARENT_COMMAND="$(ps -o comm= $PPID)"
Replace comm=
with args=
to get the full command line (command + arguments). The = alone is used to suppress the headers.
http://stackoverflow.com/a/26985984/766330
Count occurrences of a character
use grep -o
needle="," var="text,text,text,text" number_of_occurrences=$(grep -o "$needle" <<< "$var" | wc -l)
http://stackoverflow.com/a/16679459/766330
About <<<
sign, refer here strings
head and tail of a file ★
(head; tail) < file.txt
http://stackoverflow.com/a/8624829/766330
To pipe stderr, and not stdout
First redirect stderr to stdout — the pipe; then redirect stdout to /dev/null (without changing where stderr is going):
command 2>&1 >/dev/null | grep 'something’
순서를 바꾸면 안된다. 2>&1이 먼저 와야 한다.
https://stackoverflow.com/a/2342841/766330
nohup to already-running process
1
c-z bg disown -h job# eg. %1, %2, ...
jobs command shows this number.
2
c-z # @ another terminal kill -20 PID #SIGTSTP : suspend process kill -18 PID #SIGCONT
inspecting a text file
xxd hexdump -C cat -v sed -n l
Array
declare, add:
AR[idx]=VAL
zero base.
declare -a AR AR=(V1 V2 V3 VN)
echo ${AR[*]} =echo ${AR[@]}
unset AR[1]
해당 index가 비는 것. rearrange되지 않는다.
Comparing strings
String 비교시 =
앞 뒤 공백 주의. 붙여쓰면 assign.
String의 경우 =
와 ==
는 같다.
Regex: Match NOT curved or square bracket
\\[[^\\[\\]]+\\]
https://stackoverflow.com/a/15221304/766330
substring
stringZ=abcABC123ABCabc # 0123456789..... # 0-based indexing. echo ${stringZ:0} # abcABC123ABCabc echo ${stringZ:1} # bcABC123ABCabc echo ${stringZ:7} # 23ABCabc
‘Substring Extraction’ part of this document.