First script file
Copy codeThe Code is as follows :#! /Bin/bash
Echo "This is my first bash code! "
Exit 0
Redirection symbols and mathematical computationCopy codeThe Code is as follows :#! /Bin/bash
Echo-n "The time and date are :"
Date
Value1 = 100 # spaces are not allowed before and after the equal sign
Value2 = $ value1
Echo-n "value1 ="
Echo $ value1
Echo-n "value2 ="
Echo $ value2
Ls-l | sort> out.txt # pipe symbol (|) and redirect output symbol>
Ls-l> out.txt # rewrite append output symbol>
Echo-n "wc <out.txt :"
Wc <out.txt # redirect input symbol <
Echo "sort <EOF... EOF"
Sort <EOF # built-in input redirection <
'Date'
EOF
# Mathematical computing
Echo-n "expr calculation: 1 + 5 ="
Expr 1 + 5
Echo-n "calculated using square brackets: 1 + 5 ="
Echo $[1 + 5]
Echo "using the bc calculator for floating point operations"
Var1 = 1, 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 commandCopy 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
# Only integers can be compared
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 the file directory
If [-d $ HOME]
Then
Echo "your Home dir exists"
Cd $ HOME
Ls-
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 statementCopy 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 = 1, 100
Until [$ var1-eq 0]
Do
Echo $ var1
Var1 = $ [$ var1-25]
Done
# File data Loop
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> output.txt
Read ParametersCopy codeThe Code is as follows :#! /Bin/bash
Name = 'basename $0'
Echo the commane entered is: $ name
C_args =$ #
Echo count args: $ c_args
# Obtain 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? "
# Reading files
Count = 1
Cat for.txt | while read line
Do
Echo "Line $ count: $ line"
Count = $ [$ count + 1]
Done
Echo "Finished processing the file"
Redirection file descriptorCopy 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 shoshould store in the output file"
Echo "along with this line ."
Exec 1> & 3
Echo "Now things shocould 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
# Disable file descriptors
Exec 8> &-
# List file description servers
# '/Usr/sbin/lsof-a-p $' | more
# Disabling command output
#2>/dev/null
# Create a local temporary file
Tempfile = 'mktemp test. xxxxxx'
Exec 4> $ tempfile
Echo "This is the first line"> & 3
Exec 4> &-
# Create a temporary file in/temp
Tmpfile = 'mktemp-t tmp. xxxxxx'
Echo "The temp file is located at: $ tempfile"
Cat $ tempfile
Rm-f $ tempfile
# Creating temporary folders
Tmpdir = 'mktemp-d dir. xxxxxx'
Cd $ tmpdir
Tempfile1 = 'mktemp. xxxxxx'
Ls-l
Cd ..
# Record messages
A = 'date | tee testfile ;\
Cat testfile ;\
Date | tee-a testfile ;\
Cat testfile'
Signal ProcessingCopy 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
#./Test6.sh &
# Run the script without using the terminal
# Nohup./test6.sh &
# View a job
# Jobs
# Restarting a job
# Bg 2 (job serial number) // background
# Fg 2 // front-end
# Priority
# Nice-n 10./test6.sh
# Renice 10-p 25904 (process number)
# Run the at command at the expected time
# At-f test6.sh 20: 00
# Batch command, run when the average system load is less than 0.8, you can set the time, better than at command
# Corn tables can be set to run cyclically. format:
# Min hour dayofmonth month dayofweek command
# Run 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
Function usageCopy codeThe Code is as follows :#! /Bin/bash
# Functions
# Return values
Function func1
{
Read-p "Enter a value:" value
Echo $ [$ value * 2]
}
Result = 'function1'
Echo "the new value is $ result"
# Passing 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 to the function library
# ../Myfuncs