Getting started with Linux Shell scripts

Source: Internet
Author: User

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

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.