bash: syntax and commands
- array
- normal array: using integer as index
- associative array: using string as index (after BASH 4.0)
- user
- sudo <command> <arguments>: to work as root user
- file
- echo
- printf
- redirect cmd to file, redirect data can't to use for pipeline:
- cmd > out.txt; #stdout to out.txt
- cmd 2> err.txt; #stderr to err.txt
- cmd 2>err.txt 1>out.txt;
- cmd > all.txt 2>&1; cmd &> all.txt; #stdout and stderr to all.txt
- tee: redirect and to pipeline
- redirect file to cmd
- cmd < file
- cmd<< #here document
- cmd<<< #here string
- file handler
- cat: concatenate files and print to stdout
- math
- let
- (())
- []
- expr
- bc
- for easily usage of terminal
- alias
- terminal
- tput: to get some terminal infos, column number, line numbers, cursor position, color...
- stty: change terminal settings
- system resource
- time
- date
- nptdate: get time from internet
- time
- sleep
- process and Env
- pgrep <command name>: to get command process
- /proc/$PID/environ: process <PID>'s environment
- pgrep <command name>: to get command process
- time
- ENV
- export: export <env_var>: the env_var will get by any procedure launch by current shell
- arguments
- ${parameter:expression}: if parameter exist and is not empty, using the value of expression
- built-in
- length = ${#var}: get length of var
- echo $SHELL: get current shell
- echo $0: get current shell, some situations it is different with echo $SHELL
- []: it is a command
- array
-
#!/bin/bash ###basic array#### ##def array_var1=(test1 test2 test3 test4) array_var2[0]="test1" array_var2[1]="test2" array_var2[2]="test3" array_var2[3]="test4" array_var2[4]="test5" array_var2[5]="test6" ##print echo ${array_var1[0]} index=5 echo ${array_var2[$index]} echo ${array_var1[*]} echo ${array_var1[@]} # > test1 test2 test3 test4 echo ${#array_var1[*]} # > 4 echo ${!array_var1[*]} # > 0 1 2 3 ####accociative array#### ##def declare -A test_array test_array=([type1]=orange [type2]=apple) test_array[type3]=lemon echo ${!test_array[*]} echo ${test_array[*]} # > type3 type1 type2 # > lemon orange apple
-
- echo
- echo -e "<string with regexp>"
-
$ echo -e "1\t2\t3" > 1 2 3
-
- set color
- \e[1,31m: to set color red
- \e[0m: to reset color
- for text: reset=0; black=30; red=31; green=32; yellow=33; blue=34; magenta=35; cyan=36; white=37
- for background: reset=0; black=40; red=41; green=42; yellow=43; blue=44; magenta=45; cyan=46; white=47
-
$ echo -e "\e[1;31m test \e[0m" > test -
$ echo -e "\e[1;41m test \e[0m" > test
- printf
- %s, %c, %d, %f
- -: left justifying
-
$ printf "%-5s %-5s %-5s\n" This is test > This is test
- tee
- get data from stdin and redirect them to stdout and to a file
- cmd | tee FILE1 FILE2 | otherCmd
- cmd | tee - #send data twice to stdout
-
$ echo A1 > a1 $ echo A2 > a2 $ echo A3 > a3 $ cat a* | tee out.txt | cat -n > 1 A1 > 2 A2 > 3 A3 $ more out.txt > A1 > A2 > A3 $ echo add > add.txt $ cat add.txt| tee -a out.txt|cat -n #append to file out.txt > 1 add $ more out.txt > A1 > A2 > A3 > add $ echo test | tee - #redirect to stdout twice, but not redirect to any file > test > test
- <redirection>
-
#!/bin/bash # > : stdout to file # >> : append stdout to file # < : file to cmd #redirect text in script to another file # all txt between cat line and EOF line will write into log.txt cat << EOF > log.txt This is a general file. Don't edit it. add more. add more. EOF
-
- file handler
-
#!/bin/bash #create fh=3 to read from file toread.txt exec 3<toread.txt #use fh cat <&3 # > this is a test #create fh=4 to write, and write some words exec 4 > towrite.txt echo newline test >&4 #to close fh #generally, shell will close fh as long as you exit script exec 3>&- exec 4>&-
- ss
- ss
-
- <math>
-
#!/bin/bash no1=4; no2=5; let result=no1+no2 #no need $ for using let let no1++ let no1-- let no1+=6 #equal to: let no1=no1+6 result2=$[ no1 + no2] result22=$[ $no1 + 5] result3=$(( no1 +1 )) result33=$(( $no1 +1 )) result4=`expr $no1+5` ##advanced usage: bc echo "4*0.33"|bc #> 1.32 result=`echo "$no1 * 1" |bc` echo $result #> 4 echo "scale=2;22/7"|bc #> 3.14: to set decimal digits to 2 dn=100 bn=1100100 echo "obase=2;$dn"|bc #> 1100100 echo "obase=10;ibase=2;$bn"|bc #> 100 echo "sqrt(100)"|bc #> 10 echo "10^3"|bc #> 1000
-
- alias
-
#!/bin/bash alias m='more' unlias m #\command #to exec the original command rather than its alias \m # > command not found alias # > list all alias
-
- tput
-
#!/bin/bash ##col num & line num tput cols tput lines ##current terminal's name tput longname ##move cursor to (100,100) tput cup 100 100 ##set backgroud color and foreground color ## number can be 1-7 each interger is a specific color tput setb 7 tput setf 6 ##set bold tput bold ##set unline's start and end tput smul tput rmul ##set all contents after current cursor position ##?? tput ed
-
- stty
- to change and print terminal settings
-
#!/bin/bash ##to disable echo in terminal echo "Enter Password: " stty -echo read password stty echo echo echo End Set Password ##to show terminal settings echo -g # > 2d00:5:bf:......................
- date
- persion is second
- "+" the format string with prefix + is date's paramter
- "--date" indicate the date as input
- %a/%A = Sat/Saturday
- %b/%B = Nov/Novmber
- %d/%D = 31(day)/date with specific format, such as 10/11/2020
- %y/%Y = 20/2020
-
$ date > Sun Oct 11 07:06:40 PDT 2020 ##+<date's parameter> $ date +%s > 1602425200 # --date to indicate the date as input $ date --date "Sat Jan 11 2020" +%s > 1578729600 date --date "Jan 11 2020" +%A > Saturday date "+%d %b %Y" > 11 Oct 2020 #set date $ date -s "12 Oct 2022"
-
#!/bin/bash start=$(date +%s) sleep 3 end=$(date +%s) diff=$((end-start)) echo "diff time is: $diff" #> 3
- time
- to get the time of execuating a command or the resource using by this command
-
#to check how long time a command used # time cmd|script # real time: #The CPU time consumed by this cmd|script's process. # real time = 'this cmd|script's CPU time'+'this cmd|script's block time (eg: time consumed to wait IO)'+'other process's CPU time' # linux is an multiple process system, while a cmd is executed, it might that some other process's race CPU # user time: the CPU time exactly consumed by this cmd|script # system time: the CPU time consumed by kernal system call by this cmd|script $ time ls > testfile.sh test.txt ... > real 0m0.015s > user 0m0.000s > sys 0m0.004s
- fg
- er
- <process>
-
$ pgrep bash > 8111 $ cat /proc/8111/environ > USER=aaaHOME=/u/aaaPATH=/bin...... $cat /proc/8111/environ|tr '\0' '\n' > USER=aaa HOME=/u/aaa PATH=/bin ....
-
- export: export <env_var>: the env_var will get by any procedure launch by current shell
-
export PATH=/u/test:$PATH
- function to easier add a PATH
-
prepend() {[ -d "$2" ] && eval $1=\"$2\$\{$1:+':'\$$1\}\" && export $1; } #usage notes: #${parameter:+expression} #if parameter exist and is not empty, then using the value of expression
-
- [] it is a command
-
test is similar, but it is not as efficient as [], because test will always launch a new process
-
if [ $UID -ne 0 ]; then echo None root user else echo root user fi if test $UID -ne 0 then .... ....
- sss
- sss
- sss
- ss
- s
- s
- s
-

浙公网安备 33010602011771号