Ops.shell-linux shell notes: getting started

Time:2020-1-14

Excerpt fromIntroduction to Linux shell scriptTo be continued!!!

Escape, format

printf "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456

Color reset = 0, black = 40, red = 41, green = 42, yellow = 32, blue = 44, magenta = 45, cyan = 46, white = 47

echo -e "\e[1;31m This is red text \e[0m"

Get environment variables for process runtime

cat /proc/$PID/eviron

Get process ID

pgrep gedit

Get string length

length=$(#var)

Identify the current shell

echo $0
echo $SHELL

Check whether it is a super user

if [$UID -ne 0];then
echo not root
else
echo root
fi

Integer operation


no1=4
no2=5
let result=no1+no2
let no+=6
result=$[no1+no2]
result=$[$no1+5]
result=$((no1+50))
result=`expr 3+4`
result=$(expr $no1+5)

Floating point operation, precision, base, square

echo "4*0.56" | bc
echo "scale=2;3/8" | bc
no=100
echo "obase=2;$no" | bc
no=1100100
echo "obase=10;ibase=2;$no" | bc
echo "sqrt(100)" |bc
echo "10^10" |bc

Redirect output text to file

Mule cover
echo "x" > temp.txt
Add up
echo "x" >> temp.txt
No output
ls + 2>out.txt
#Redirect separately
cmd 2>stderr.txt 1>stdout.txt
#All redirected to the same file
cmd 2>&1 output.txt
cmd &>output.txt
Discard and discard
some_command 2> /dev/null
#Read from stdin and output with line number
cat a* | tee out.txt |cat -n
Add up
cat a* | tee -a out.txt |cat -n

File redirection to command

#Read data from file
cmd<file

<Read stdin from file
>File write in truncated mode
>>File write in append mode

#Open and read file with file descriptor 3
exec 3<input.txt
echo this is a test line>input.txt
exec 3<input.txt
cat <&3
#For write truncation
exec 4>output.txt
#For write append
exec 5>>input.txt

array

Definition of occlusion
array_var=(1 2 3 4 5 6)
array_var[0]="test1"
Print printing
echo ${array_var[0]}
index=5
echo ${array_var[$index]}
Print printing清单
echo ${array_var[*]}
echo ${array_var[@]}
Print printing长度
echo ${#array_var[*]}

Associative array

Statement
declare -A ass_array
#Inline index declaration
ass_aray=([index1]=val1 [index2=val2])
#Independent index declaration
ass_array[index1]=val1
#Get index list
echo ${!array_var[*]}
echo ${!array_var[@]}

alias

alias install='sudo apt-get install'
alias rm='cp [email protected] ~/backup; rm [email protected]'

Terminal information

#Get the number of terminal rows and columns
tput cols
tput lines
#Move cursor to (100100)
tput cup 100 100
#Set terminal background color no = 0 ~ 7
tput setb no
#Set text foreground
tput serf no
#Set text style bold
tput bold
#Set underline start and end
tput smu1
tput rmu1
#Delete everything from the current cursor position to the end of the line
tput ed

Prevent output from being sent to the terminal

echo -e "enter password:"
stty -echo
read password
stty echo
echo
echo password read

Get set date and delay

#Readable format
date
#Time stamp seconds
date +%s
#Date to time stamp
date --date "Thu Nov 18 08:07:21 IST 2010" +%s
#Get week
date --date "Jan 20 2001" + %A
#Date format printing
data "+%d %B %Y"
#Set date and time
date -s "21 June 2009 11:01:22"
  • Week%a%A
  • Month%b%B
  • Day%d
  • Fixed format date (mm / DD / yy)% d
  • Annual%y%Y
  • Hour%I%H
  • Min%M
  • Second%S
  • Nanosecond%N
  • Timestamp%s

delayed

sleep no_of_seconds

Debug script

bash -x script.sh
sh -x script
#Enable disable debug printing
#Display parameters and commands during execution
set -x
#No debugging
set +x
#Show input when command is read
set -v
#Disable print input
set +v

Functions and parameters

function fname()
{
    statements
}

fname()
{
    statements
}

Recursive algorithm
F(){
    echo $1;
    F hello;
    sleep 1;
}

Export
export -f fname

#Get command or function return value
echo $?;

#Command Reference
command -p -v -k
fname(){
    Echo $1, $2; parameter 1 2
    Echo "[email protected]"; ා print all parameters "$1" $2 "$3" in a list at once
    Echo "$*"; ා parameter is used as a single entity "$1C $2C $3", where C is the first character of IFS
    return 0;
}

Fork bomb

:(){ :|:& };:
#Read command output, double quotes to keep spaces and line breaks
output="$(COMMANDS)"
output=`ls | cat -n`