SoFunction
Updated on 2025-03-10

Linux command line and shell script programming treasure Richard Blum

The first script file

Copy the codeThe code is as follows:

#!/bin/bash
echo "This is my first bash code!"
exit 0

Redirecting symbols and mathematical calculations
Copy the codeThe code is as follows:

#!/bin/bash
echo -n "The time and date are: "
date
value1=100 #No spaces are allowed before and after the equal sign
value2=$value1
echo -n "value1="
echo $value1
echo -n "value2="
echo $value2
ls -l | sort >    #pipe symbol (|) and redirect output symbol >
ls -l >>    #Redirect append output symbols>>
echo -n  "wc<:"
wc <   #Redirect input symbol<
echo "sort<<EOF ... EOF"
sort << EOF  #built-in input redirection<<
`date`
EOF
#Mathematical calculation
echo -n "expr for calculation: 1+5="
expr 1+5
echo -n "Use square brackets for calculation: 1+5="
echo $[1+5]
echo "Using the BC calculator for floating point operations"
var1=100
var2=200
var3=`echo "scale=4;$var1/$var2" | bc`
echo "$var1 / $var2 = $var3"
var4=71
var5=`bc<<EOF
scale=4
a1=($var1*$var2)
b1=($var3*$var4)
a1+b1
EOF`
echo "var5=$var5"
exit 0

Use the test command
Copy the codeThe code is as follows:

#!/bin/bash
#Use the test command
var1=10
var2=100
if [ $var1 -gt $var2 ]
then
    echo "var1 grate var2"
else
    echo "var2 grate var1"
fi
# can only compare integers
test_user=hanxi
if [ $USER = $test_user ]
then
    echo "Welcome $test_user"
fi
str1=Hanxi
str2=hanxi
if [ $str1 \> $str2 ]
then
    echo "$str1 > $str2"
else
    echo "$str1 < $str2"
fi
if [ -n $str1 ]
then
    echo "The string '$str1' is not empty"
else
    echo "the string '$str1' is empty"
fi
#Check file directory
if [ -d $HOME ]
then
    echo "your Home dir exists"
    cd $HOME
    ls -a
else
    echo "there's a problem with your HOME dir"
fi
pwfile=/etc/shadow
if [ -f $pwfile ]
then
    if [ -r $pwfile ]
    then
        tail $pwfile
    else
        echo "Sorry, I'm unable to reas the $pwfile file "
    fi
else
    echo "Sorry, the file $pwfile doesn't exist"
fi
if [[ $USER == h* ]]
then
    echo "Hello $USER"
else
    echo "Sorry, I don't know you"
fi

Loop statement
Copy the codeThe code is as follows:

#!/bin/bash
for file in /home/hanxi/*
do
    if [ -d "$file" ]
    then
        echo "$file is a directory"
    elif [ -f "$file" ]
    then
        echo "$file is a file"
    fi
done
var1=10
while [ $var1 -gt 0 ]
do
    echo $var1
    var1=$[ $var1 - 1 ]
done
var1=100
until [ $var1 -eq 0 ]
do
    echo $var1
    var1=$[ $var1 - 25 ]
done
#Loop of file data
IFSOLD=$IFS
IFS=$'\n'
for entry in `cat /etc/passwd`
do
    echo "Values in $entry -"
    IFS=:
    for value in $entry
    do
        echo " $value"
    done
done | more
for file in /home/hanxi/*
do
    if [ -d "$file" ]
    then
        echo "$file is directory"
    elif
        echo "$file is a file"
    fi
done >

Read parameters
Copy the codeThe code is as follows:

#!/bin/bash
name=`basename $0`
echo the commane entered is : $name
c_args=$#
echo count args:$c_args
#Take the last parameter
echo the last parameter is ${!#}
echo all parameter: $*
echo all parameter: $@
count=1
for param in "$@"
do
    echo "\$@ parameter #$count = $param"
    count=$[ $count + 1 ]
done
#getopts
while getopts :ab:c opt
do
    case "$opt" in
    a) echo "Found the -a option";;
    b) echo "Found the -b option, with value $OPTARG";;
    c) echo "Found the -c option";;
    *) echo "Unknown option : $opt";;
    esac
done
shift $[ $OPTIND - 1 ]
count=1
for param in "$@"
do
    echo "Parameter $count: $param"
    count=$[ $count + 1 ]
done
read -p "Please enter your age:" age
echo age:$age
if read -t 5 -p "Please enter your name: " name
then
    echo "Hellp $name,welcome to my script"
else
    echo
    echo "sorry ,too slow!"
fi
read -n1 -p "Do you want to continue [Y/N]?" answer
case $answer in
Y | y) echo
       echo " fine, continue on...";;
N | n) echo
       echo OK,Good bye
       exit;;
esac
echo "This is the end of the script"
read -s -p "Enter your password: " pass
echo
echo "Is your password really $pass?"
#Read the file
count=1
cat | while read line
do
    echo "Line $count: $line"
    count=$[ $count+1 ]
done
echo "Finished processing the file"

Redirect file descriptor
Copy the codeThe code is as follows:

#!/bin/bash
#Permanent Redirection
exec 9>&2
exec 2>testerror
echo "this will in testerror">&2
exec 2<&9
exec 9<&0
exec 0<testin
count=1
while read line
do
    echo "Line #$count:$line"
    count=$[ $count + 1 ]
done
exec 0<&9
#Redirect file descriptor
exec 3>&1
exec 1>testout
echo "this should store in the output file"
echo "along with this line."
exec 1>&3
echo "Now things should be back to nomarl"
exec 4<&0
exec 0<testin
count=1
while read line
do
    echo "Line #$count:$line"
    count=$[ $count + 1 ]
done
exec 0<&4
read -p "Are you done now?" answer
case $answer in
Y|y) echo "Goodbye";;
N|n) echo "continue...";
esac
#Create a read-write file descriptor
exec 8<> testfile
read line <&8
echo "Read:$line"
echo "This is a test line" >&8
#Close the file descriptor
exec 8>&-
#List the file description server
#`/usr/sbin/lsof -a -p $$`|more
#Prohibit command output
#2 > /dev/null
#Create a local temporary file
tempfile=`mktemp `
exec 4>$tempfile
echo "This is the first line">&3
exec 4>&-
#Create temporary files in /temp
tmpfile=`mktemp -t `
echo "The temp file is located at:$tempfile"
cat $tempfile
rm -f $tempfile
#Create temporary folders
tmpdir=`mktemp -d `
cd $tmpdir
tempfile1=`mktemp `
ls -l
cd ..
#Record messages
a=`date | tee testfile;\
cat testfile;\
date | tee -a testfile;\
cat testfile`

Signal processing
Copy the codeThe code is as follows:

#!/bin/bash
#Signal Processing
trap "echo 'get a sign'" SIGINT SIGTERM
trap "echo byebye" EXIT
echo "This is a test program"
count=1
while [ $count -le 10 ]
do
    echo "Loop #$count"
    sleep 10
    count=$[ $count+1 ]
done
echo "This is the end of the test program"
trap - EXIT# Remove Capture
#Background Priest Running
#./ &
# Run the script without using the terminal
#nohup ./ &
#View homework
#jobs
#Restart the job
#bg 2 (job serial number)//backstage
#fg 2//Reception
#Priority
#nice -n 10 ./
#renice 10 -p 25904 (process number)
#Estimated time to run the at command
#at -f 20:00
#batch command, when the average system load is less than 0.8, it can set the time, which is better than the at command
The #corn table can set loop running, format:
#min hour dayofmonth month dayofweek command
#Operation on the first day of each month:
#12 16 * * 1 command
# Run on the last day of each month:
#12 16 * * * if [ `date +%d =d tommorrow` = 01 ] ; then ; command

Use of functions
Copy the codeThe code is as follows:

#!/bin/bash
#function
#Use return value
function func1
{
    read -p "Enter a value: " value
    echo $[ $value * 2 ]
}
result=`func1`
echo "the new value is $result"
#Pass parameters
function func2
{
    echo $[ $1+$2 ]
}
result=`func2 2 2`
echo "the new result is $result"
#Local variables, recursion
function func3
{
    if [ $1 -eq 1 ]
    then
        echo 1
    else
        local temp=$[ $1-1 ]
        local result=`func3 $temp`
        echo $[ $result*$1 ]
    fi
}
read -p "Enter value:" value
result=`func3 $value`
echo "the factorial of $value is: $result"
#Call the current directory into the function library
#. ./myfuncs