Script receive options for shell scripts

Source: Internet
Author: User

1. Create a script that can add script information to other scripts:
#vim cs.sh
#!/bin/bash
Cat > $ <<eof
#!/bin/bash
#Name: ' BaseName $ '
#Description:
#Author: TEST
#Version: 0.0.1
#Datatime: ' Date ' +%f +%t "'
#Usage: ' BaseName $ '

Eof
Vim +8 $

2. Modify the script, if there is content in the open script, open it directly, if it is empty, add the information.
#vim cs.sh
#!/bin/bash
if! grep "[^[::space]]" $ &>/dev/null; Then
Cat > $ <<eof
#!/bin/bash
#Name: ' BaseName $ '
#Description:
#Author: TEST
#Version: 0.0.1
#Datatime: ' Date ' +%f +%t "'
#Usage: ' BaseName $ '

Eof
Vim +8 $
Fi
Vim + $

3. Modify the script and add the grammar test function. If there is an error, let the user choose whether to quit editing.
#vim cs.sh
#!/bin/bash
if! grep "[^[::space]]" $ &>/dev/null; Then
Cat > $ <<eof
#!/bin/bash
#Name: ' BaseName $ '
#Description:
#Author: TEST
#Version: 0.0.1
#Datatime: ' Date ' +%f +%t "'
#Usage: ' BaseName $ '

Eof
Vim +8 $
Fi
Vim + $

Until Banshi-n $ &>/dev/null;d o
Read-p "Syntax error, q| Q for Quit,others for edit. " Opt
Case $ in {
q| Q)
echo "Quit"
Exit 5
;;
*)
Vim + $
;;
}
Esac
Done

chmod +x $

4. Use the cs.sh to create a script and add options to the script. :
#./cs.sh opt.sh
#!/bin/bash
#Name: opt.sh
#Description:
#Author: TEST
#Version: 0.0.1
#Datatime: xxxx-xxxx
#Usage: opt.sh
function USAGE () {
echo "Usage:opt.sh [-b|-d] args"

}
While getopts ": b:d:" SW;d O
Case $SW in
b
echo "The option is B"
Echo $OPTARG
;;
D
echo "The option is D"
Echo $OPTARG
;;
\?)
echo "Wrong Options"
USAGE
;;
Esac
Done
#./opt.sh-b test1
#./opt.sh-d test2
#./opt.sh-c test3

5. Modify the sc.sh and use the option to let the user enter description when creating the script.
#vim cs.sh
#!/bin/bash
While getopts ":d:" Sw;do
Case $SW in
D
Desc= $OPTARG
\?)
echo "Usage:sc.sh [-D DESCRIPTION] FILENAME"
Esca
Done

Shift $[$OPTIND-1]

if! grep "[^[::space]]" $ &>/dev/null; Then
Cat > $ <<eof
#!/bin/bash
#Name: ' BaseName $ '
#Description: $DESC
#Author: TEST
#Version: 0.0.1
#Datatime: ' Date ' +%f +%t "'
#Usage: ' BaseName $ '

Eof
Vim +8 $
Fi
Vim + $

Until Banshi-n $ &>/dev/null;d o
Read-p "Syntax error, q| Q for Quit,others for edit. " Opt
Case $ in {
q| Q)
echo "Quit"
Exit 5
;;
*)
Vim + $
;;
}
Esac
Done

chmod +x $

6. Edit the opt.sh to see the specifics of the optind.
#vim opt.sh
#!/bin/bash
#Name: opt.sh
#Description:
#Author: TEST
#Version: 0.0.1
#Datatime: xxxx-xxxx
#Usage: opt.sh
function USAGE () {
echo "Usage:opt.sh [-b|-d] args"

}
While getopts ": BD" SW;d O
Case $SW in
b
echo "The option is B"
Echo $OPTARG
Echo $OPTIND
;;
D
echo "The option is D"
Echo $OPTARG
Echo $OPTIND
;;
\?)
echo "Wrong Options"
USAGE
;;
Esac
Done
#./opt.sh-b
2
#./opt.sh-b-D
2
3
7. Write a script ift.sh, you can accept the option I I a, the requirements are as follows:
Usage is as follows: ift.sh [-I interface|-i ip|-a]
-I displays the IP address of the specified network card,-I displays the interface corresponding to the IP address,-a displays all interfaces and IP information outside the IO interface.

#vim ift.sh
#!/bin/bash
Showip () {
if! Ifconfig|grep-o "^[^[:space:]]\{1,\}" |grep &>/dev/null; Then
Return 15
Fi

Echo-n "${1}:"
Ifconfig $ |grep-o "inet addr:[0-9\." \{1,\} "|cut-d:-f2
Echo
}

Showif () {
if! Ifconfig |grep-o "inet addr:[0-9\. \{1,\} "|cut-d:-f2|grep $ &>/dev/null;then
return 16
Fi

Echo-n "${1}:"
Ifconfig |grep-b 1 "$" |grep-o "^[^[:space:]]\{1,\}"
Echo
}

ShowAll () {
Don't want to come out for the time being empty first!! Later


}

While getopts ": i:i:a:" SW;d O
Case $SW in
I
Showip $OPTARG
If [$?-eq] && echo "Wrong interface!"
;;
I)
showif $OPTARG
f [$-eq] && echo "Wrong ip!"
;;
A
ShowAll
;;

\?)
echo "Usage ift.sh [-I interface|-i ip| -A] "
;;
Esac






#getopts "[:]option[:] ..." [Optarg]
Built-In Parameters:
Optarg parameters
Optind the option index, when there are multiple options in the script, the first point is to the second option, note that it is not a. Use shift to point to the third option, and so on.

The option option can have multiple if the parameter has the OPTARG option built-in parameters behind it, and if the post parameter is followed, the option is preceded by a colon (:).
If you do not want to output an error message, precede all options with a colon (:).
Note that only one option can be followed at a time and cannot be followed by more than one.


This article is from "Small Private blog" blog, please be sure to keep this source http://ggvylf.blog.51cto.com/784661/1668732

Script receive options for 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.