Ops.shell-Linux shell notes: introduction

Posted by MSK7 on Wed, 09 Oct 2019 09:04:58 +0200

Excerpt from "Introduction to Linux shell scripts" is not finished!!!

Conversion, Formatting

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 when the process is running

cat /proc/$PID/eviron

Get the process id

pgrep gedit

Get string length

length=$(#var)

Identify the current shell

echo $0
echo $SHELL

Check if it is a superuser

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 Operations, Accuracy, Number, 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

Output text redirected to a file

#cover
echo "x" > temp.txt
#Append
echo "x" >> temp.txt
#No output
ls + 2>out.txt
# Individual redirection
cmd 2>stderr.txt 1>stdout.txt
# Are redirected to the same file
cmd 2>&1 output.txt
cmd &>output.txt
#Discard
some_command 2> /dev/null
#Read from stdin, add line number output
cat a* | tee out.txt |cat -n
#Append
cat a* | tee -a out.txt |cat -n

File redirection to command

#Read data from files
cmd<file

Read stdin from file
> File Writing in Truncated Mode
File Writing in Additional Mode

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

array

#Definition
array_var=(1 2 3 4 5 6)
array_var[0]="test1"
#Printing
echo ${array_var[0]}
index=5
echo ${array_var[$index]}
#Print list
echo ${array_var[*]}
echo ${array_var[@]}
#Print length
echo ${#array_var[*]}

Associative array

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

alias

alias install='sudo apt-get install'
alias rm='cp $@ ~/backup; rm $@'

Terminal information

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

Prevent sending output to terminal

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

Get the setup date and delay

#Readable format
date
#Timestamp seconds
date +%s
#Date-to-date timestamp
date --date "Thu Nov 18 08:07:21 IST 2010" +%s
#Get week
date --date "Jan 20 2001" + %A
#Date formatting printing
data "+%d %B %Y"
#Setting 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 Prohibit Debugging Printing
#Display parameters, commands at execution time
set -x
#No debugging
set +x
#Display input when commands are read
set -v
#Prohibiting Print Input
set +v

Functions and parameters

function fname()
{
    statements
}

fname()
{
    statements
}

#recursion
F(){
    echo $1;
    F hello;
    sleep 1;
}

#export
export -f fname

#Get the command or function return value
echo $?;

#Command Reference
command -p -v -k  
fname(){
    echo $1,$2;#Parameter 12
    echo "$@";#Print all parameters'$1','$2','$3'at once in a list format
    echo "$*";#The parameter is treated as a single entity "$1c"c", where C is the first character of IFS.
    return 0;
}

fork bomb

:(){ :|:& };:
#Read command output, double quotation marks to reserve spaces and newline characters
output="$(COMMANDS)"
output=`ls | cat -n`

Topics: Linux shell sudo