Linux command line and shell script Cheng Richard blum_linux Shell

Source: Internet
Author: User
Tags time and date

First script file

Copy Code code as follows:

#!/bin/bash
echo "This are my I-A-bash code!"
Exit 0

redirect Symbols and mathematical calculations
Copy Code code as follows:

#!/bin/bash
Echo-n "The time and date are:"
Date
value1=100 #等号前后不允许出现空格
value2= $value 1
Echo-n "Value1="
echo $value 1
Echo-n "Value2="
Echo $value 2
Ls-l | Sort > OUT.txt #管道符号 (|) and redirect Output symbols >
Ls-l >> out.txt #重定向追加输出符号 >>
Echo-n "Wc<out.txt:"
WC < OUT.txt #重定向输入符号 <
echo "Sort<<eof ... EOF "
Sort << EOF #内置输入重定向 <<
' Date '
Eof
#数学计算
Echo-n "Expr for calculation: 1+5="
Expr 1+5
Echo-n "Using square brackets for calculation: 1+5="
Echo $[1+5]
echo "uses the BC calculator for floating-point operations"
var1=100
var2=200
Var3= ' echo ' scale=4 $var 1/$var 2 "| BC '
echo "$var 1/$var 2 = $var 3"
var4=71
Var5= ' bc<<eof
Scale=4
a1= ($var 1* $var 2)
b1= ($var 3* $var 4)
A1+b1
EOF '
echo "var5= $var 5"
Exit 0

Using the test command
Copy Code code as follows:

#!/bin/bash
#使用test命令
var1=10
var2=100
If [$var 1-gt $var 2]
Then
echo "Var1 Grate Var2"
Else
echo "Var2 Grate var1"
Fi
#只能比较整数
Test_user=hanxi
if [$USER = $test _user]
Then
echo "Welcome $test _user"
Fi
Str1=hanxi
Str2=hanxi
If [$str 1 \> $str 2]
Then
echo "$str 1 > $str 2"
Else
echo "$str 1 < $str 2"
Fi
If [-N $str 1]
Then
echo "The string ' $str 1 ' is not empty"
Else
echo "The string ' $str 1 ' 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

Loop statement
Copy Code code as follows:

#!/bin/bash
For file in/home/hanxi/*
Todo
If [-D "$file]"
Then
echo "$file is a directory"
elif [F "$file]"
Then
echo "$file is a file"
Fi
Done
var1=10
While [$var 1-GT 0]
Todo
echo $var 1
var1=$[$var 1-1]
Done
var1=100
Until [$var 1-eq 0]
Todo
echo $var 1
var1=$[$var 1-25]
Done
#文件数据的循环
Ifsold= $IFS
ifs=$ ' \ n '
For entry in ' CAT/ETC/PASSWD '
Todo
echo "Values in $entry-"
Ifs=:
For value in $entry
Todo
echo "$value"
Done
Done | More
For file in/home/hanxi/*
Todo
If [-D "$file]"
Then
echo "$file is directory"
Elif
echo "$file is a file"
Fi
Done > Output.txt

Read parameters
Copy Code code as follows:

#!/bin/bash
Name= ' basename $ '
echo the Commane entered is: $name
c_args=$#
echo Count args: $c _args
#取最后一个参数
echo the last parameter is ${!#}
echo All parameter: $*
echo All parameter: $@
Count=1
For param in "$@"
Todo
echo "\$@ parameter # $count = $param"
count=$[$count + 1]
Done
#getopts
While Getopts:ab:c opt
Todo
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 "$@"
Todo
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:"
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?"
#读取文件
Count=1
Cat For.txt | While Read line
Todo
echo "line $count: $line"
count=$[$count +1]
Done
echo "Finished processing the file"

REDIRECT File descriptor
Copy Code code as follows:

#!/bin/bash
#永久重定向
EXEC 9>&2
EXEC 2>testerror
echo "This'll in Testerror" >&2
EXEC 2<&9
EXEC 9<&0
EXEC 0<testin
Count=1
While Read line
Todo
echo "Line # $count: $line"
count=$[$count + 1]
Done
EXEC 0<&9
#重定向文件描述符
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 is back to Nomarl"
EXEC 4<&0
EXEC 0<testin
Count=1
While Read line
Todo
echo "Line # $count: $line"
count=$[$count + 1]
Done
EXEC 0<&4
Read-p "Are You do Now" answer
Case $answer in
Y|y) echo "Goodbye";;
N|n) echo "Continue ...";
Esac
#创建读写文件描述符
EXEC 8<> testfile
Read Line <&8
echo "Read: $line"
echo "This is a test line" >&8
#关闭文件描述符
EXEC 8>&-
#列出文件描述服
# '/usr/sbin/lsof-a-P $$ ' |more
#禁止命令输出
#2 >/dev/null
#创建本地临时文件
tempfile= ' mktemp test. XXXXXX '
EXEC 4> $tempfile
echo "This is the" ">&3"
EXEC 4>&-
Create temporary files #在/temp
Tmpfile= ' mktemp-t tmp. XXXXXX '
echo "The temp file is located at: $tempfile"
Cat $tempfile
Rm-f $tempfile
#创建临时文件夹
Tmpdir= ' mktemp-d dir. XXXXXX '
CD $tmpdir
tempfile1= ' Mktemp temp. XXXXXX '
Ls-l
Cd..
#记录消息
A= ' date | Tee testfile;\
Cat testfile;\
Date | Tee-a testfile;\
Cat Testfile '

Signal Processing
Copy Code code as follows:

#!/bin/bash
#信号处理
Trap "Echo ' Get a sign '" SIGINT sigterm
Trap "Echo Byebye" EXIT
echo "This are a test program"
Count=1
While [$count-le 10]
Todo
echo "Loop # $count"
Sleep 10
count=$[$count +1]
Done
echo "This is the" ' The ' Test program '
trap-exit# Removal capture
#后台牧师运行
#./test6.sh &
#不使用终端的情况下运行脚本
#nohup./test6.sh &
#查看作业
#jobs
#重新启动作业
#bg 2 (Job serial number)//Backstage
#fg 2//Front Desk
#优先级
#nice-N/test6.sh
#renice 10-p 25904 (process number)
#预计时间运行at命令
#at-F test6.sh 20:00
#batch命令, the system runs at an average load of less than 0.8 and can set the time, better than the AT command
#corn表格可以设定循环运行, Format:
#min hour dayofmonth Month DayOfWeek command
#每个月第一天运行:
#12 * * 1 command
#每个月最后一天运行:
#12 * * If [' Date +%d =d tommorrow ' = 01]; then; Command

Use of functions
Copy Code code as follows:

#!/bin/bash
#函数
#使用返回值
function func1
{
Read-p "Enter A value:" Value
Echo $[$value * 2]
}
result= ' func1 '
echo "The new value is $result"
#传递参数
function Func2
{
Echo $[$1+$2]
}
Result= ' Func2 2 2 '
echo "The new result is $result"
#局部变量, 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"
#调用当前目录下到函数库
#. ./myfuncs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.