Shell script basic notes (1)

Posted by loganbest on Thu, 23 May 2019 20:50:24 +0200

1. Command Segmentation in Bash

Each command or sequence of commands is separated by semicolons (;) or newline characters (\n).

$ cmd1 ;cmd2 

Equivalent to

$cmd1

$cmd2

2. Terminal printing echo and printf

Double quotation marks - variable name replacement; special character order escape character () - e takes effect

Single quotation marks - - variable names will not be extended and output as they are; special character order escape characters () - e will take effect

m11@xubuntu:~$ echo -e "aaa\nbbb"
aaa
bbb
m11@xubuntu:~$ echo -e 'aaa\nbbb'
aaa
bbb

printf - Print strings in format

root@dxx-VirtualBox:~# printf "%-5s %-10s  %-4.2f\n" $var bbb 1.123456 
aaaa  bbb         1.12

3, variables

3.1. Variable assignment and equivalence

var=value

var = value

3.2. Variable Inheritance

export variable name: Any application executed by the current shell script inherits this variable

3.3. Get variable length ${# variable name}

3.4. Check if root user $UID = 0 superuser?

3.5. Modify the prompt character of bash: $SP1

3.6. Judging whether the variable is empty

${parameter:+expression}: If the parameter is empty, use the expression value

aaa="bbbb"&&echo ${aaa:+$aaa"----"}

4, calculation

4.1 let,$(()),$[] 

The $character in the calculated variable is optional.

root@xubuntu:/home/m11/dxx/test/test_shell# aa=111;bb=222 && let rs=aa*bb && echo $rs
24642
root@xubuntu:/home/m11/dxx/test/test_shell# aa=111;bb=222 && let rs=$aa*bb && echo $rs
24642

root@xubuntu:/home/m11/dxx/test/test_shell# aa=111;bb=222 && let rs=[aa*bb] && echo $rs
bash: let: rs=[aa*bb]: syntax error: operand expected (error token is "[aa*bb]")
root@xubuntu:/home/m11/dxx/test/test_shell# aa=111;bb=222 && let rs=$[aa*bb] && echo $rs
24642
root@xubuntu:/home/m11/dxx/test/test_shell# aa=111;bb=222 && let rs=$[$aa*bb] && echo $rs
24642

root@xubuntu:/home/m11/dxx/test/test_shell# aa=111;bb=222 && let rs=(($aa*bb)) && echo $rs
bash: syntax error near unexpected token `('
root@xubuntu:/home/m11/dxx/test/test_shell# aa=111;bb=222 && let rs=$(($aa*bb)) && echo $rs
24642
root@xubuntu:/home/m11/dxx/test/test_shell# aa=111;bb=222 && let rs=$((aa*bb)) && echo $rs
24642

4.2 bc Advanced Mathematical Operations

echo "4 * 5" |bc
echo "scale=10;3/8 " |bc        Setting decimal precision
aaa=10;echo "obase=2;$aaa " |bc Binary conversion
echo "sqrt(100)" |bc            square root
echo "10^2" |bc                 square

5. Redirection

0     stdin

1     stdout

2     stderr

> Only redirect the standard output, equivalent to 1 >

2> Only redirect the error output

2 > xxx1 > xxx1. Output errors to XXX and standards to xxx1

1 > Test 2 > & 1 will output the standard to test and pass the error output to the 1 output channel is equivalent to &> test

tee) redirects data to a file and provides a copy of the data as stdin for subsequent commands

Cat > END treats data between ENDs as data

#!/bin/bash
cat<<END
<xml>adsfasdf
<h1>asdfasdf
</h1>
</xml>
END

6, array

1) Ordinary arrays

Definition, use, assignment, printing

arry=(1  2 3 4 )
root@xubuntu:/home/m11/test# echo ${arry[2]}
3
root@xubuntu:/home/m11/test# echo ${arry[*]}
1 2 3 4
root@xubuntu:/home/m11/test# echo ${arry[@]}
1 2 3 4
root@xubuntu:/home/m11/test# arry[1]=12
root@xubuntu:/home/m11/test# echo ${arry[@]}
1 12 3 4
root@xubuntu:/home/m11/test# echo ${#arry[*]}
4

2) associative arrays (must be declared first)

root@xubuntu:/home/m11/test# declare -A ass_arry
root@xubuntu:/home/m11/test# ass_arry=([name]="dxx" [age]="12")
root@xubuntu:/home/m11/test# echo ${ass_arry[name]}
dxx
root@xubuntu:/home/m11/test# echo ${!ass_arry[*]}
name age
root@xubuntu:/home/m11/test# echo ${!ass_arry[@]}
name age

7. Alias

root@xubuntu:/home/m11/test# Alias LH ='ls-lh'defines aliases
root@xubuntu:/home/m11/test# unalias lh deletes aliases

Ignore aliases, use escape characters', format command

8, command

pgrep process name: Get the PID of the process

Cat/proc/1977/environ: View the environment variables of the 1977 process

root@dxx-VirtualBox:~# pgrep nm-applet
1977
root@dxx-VirtualBox:~# cat /proc/1977/environ 
GNOME_KEYRING_PID=USER=dxxLANGUAGE=en_USLC_TIME=zh_CN.UTF-8XDG_SEAT=seat0JAVA_TOOL_OPTIONS=-javaagent:/usr/share/java/jayatanaag.jar XDG_SESSION_TYPE=x11COMPIZ_CONFIG_PROFIL

Topics: VirtualBox xml shell Java