Linux shell script format
#!/bin/sh
#..... Comments
Command...
Command...
Set permissions after creating with VI chmod +x filename.sh
Execute command:./filename.sh
Local variables in the shell
The declaration of a variable, assignment, without a $ symbol, such as:
Myurl= "ABC"
Myvar= "ASD"
Note that there can be no spaces on either side of the equals sign.
Output variable Value
echo "Myurl:${myurl}"
echo "Myvar:${myvar}"
Normally when invoking a shell script, external arguments are required, and externally passed parameters can be received via $[1...N] such as:
echo $
echo "Parameter 2:${2}"
To receive parameters from the keyboard:
echo "Please input test:"
Read txt
Echo ${txt}
Traverse the current directory output file name:
for filename in LS *.jpg
Do
Echo $filename
Done
for filename in ls-l
Do
Ls-l $filename | awk ' {print $9 ' \ t ' $ '
Done
Determine the directory or file
for filename in./*
Do
If Test-f $filename
Then
echo $filename is file
Fi
If test-d $filename
Then
Echo $filename is Mulu
Fi
Done
Traverse the directory to output all the files in the directory:
function Read_dir () {
For file in ' ls $ '
Do
If test-d $ "/" $file
Then
Read_dir "/" $file
Elif test-f "/" $file
Then
echo "/" $file
Else
echo "None"
Fi
Done
}
echo "Please input Mulu:"
Read txt
Read_dir $txt
Getting started with Linux Shell scripts