Example Analysis of Linux Command Line and shell script programming
Editor to share with you the Linux command line and shell script programming example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
First script file
The code is as follows:
#! / bin/bash
Echo "This is my first bash code!"
Exit 0
Redirection symbol and Mathematical calculation
The code is as follows:
#! / bin/bash
Echo-n "The time and date are:"
Date
Spaces are not allowed before and after the value1=100 # 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 # redirect append output symbol > >
Echo-n "wc $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 #检查文件目录 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 循环语句 代码如下: #!/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 #文件数据的循环 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 parameters
The 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-an 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 hand 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 files
Count=1
Cat for.txt | while read line
Do
Echo "Line $count: $line"
Count=$ [$count+1]
Done
Echo "Finished processing the file"
Redirect file descriptor
The code is as follows:
#! / bin/bash
# permanent redirection
Exec 9 > & 2
Exec 2 > testerror
Echo "this will in testerror" > & 2
Exec 2: 3
Exec 4 > &-
# create temporary files in / temp
Tmpfile= `mktemp-t tmp.XXXXXX`
Echo "The tempfile is located at:$tempfile"
Cat $tempfile
Rm-f $tempfile
# create a temporary folder
Tmpdir= `mktemp-d dir.XXXXXX`
Cd $tmpdir
Tempfile1= `mktemp temp.XXXXXX`
Ls-l
Cd..
# logging messages
A = `date | tee testfile;\
Cat testfile;\
Date | tee-a testfile;\
Cat testfile`
Signal processing.
The 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
# Reverend running at background
#. / test6.sh &
# run a script without using a terminal
# nohup. / test6.sh &
# View Job
# jobs
# restart the job
# bg 2 (job serial number) / / backend
# fg 2max / Front desk
# priority
# nice-n 10. / test6.sh
# renice 10-p 25904 (process number)
# estimated time to run the at command
# at-f test6.sh 20:00
# batch command, which runs when the average system load is less than 0.8. You can set the time, which is better than at command.
# corn table can be set to run in a loop, in a 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
The use of functions
The code is as follows:
#! / bin/bash
# function
# use return values
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, recursive
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
The above is all the content of the article "sample Analysis of Linux Command Line and shell scripting programming". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!