Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Bash Getopts - let your script supports command line parameters     - Installation and use the telnet command under CentOS 6.x (Linux)

- Summary Linux operating system some tips to prevent attacks (Linux)

- The correct way to open Xcode - Debugging (Programming)

- PL / SQL -> UTL_FILE use presentation package (Database)

- C ++ How to determine the types of constants (Programming)

- CentOS 6.6 command-line automatic completion (Linux)

- Depth understanding of C language (Programming)

- LVM management reduces swap partition space to the root partition (Linux)

- Select helps secure the system network management tools (Linux)

- How the program is executed (Programming)

- Using shell users or virtual users to login to pureftpd (Linux)

- Linux6.1 / 6.5 dual-NIC Binding (Linux)

- CentOS7 management of systemd (Linux)

- Oracle 11g RMAN virtual private directory (Database)

- Linux with Windows Explorer as a security system (Linux)

- pureftpd basis: Install, configure, implement, anonymous logon (Linux)

- Linux virtual memory and physical memory (Linux)

- How to install Virtualbox 4.3.20 in Ubuntu 14.04 (Linux)

- Linux modify environment variables method (Linux)

- HAProxy performance under high concurrency (Server)

 
         
  Bash Getopts - let your script supports command line parameters
     
  Add Date : 2018-11-21      
         
         
         
  I used to always want to know how to create a command-line argument for my Bash script. After searching, I found the two functions can deal with this problem, getopt and getopts function function. I have no intention of arguing which function better. getopts is a shell built-in command, and it seems easier to implement than the getopt this function, so in this article I am ready to talk about getopts.

bash getopts

At first, I just tried to deal with the script command line arguments passed. Finally, I added some useful features additional functions, so that the script can be the beginning of a template for any other interactive script processed the command line. I also added a help function in plain text format, so that the script easier to read.

To a long period of its literal interpretation getopts in bash how this works, I think it is better to direct the script to have a working people feel more relaxed.

#! / Bin / bash
################################################## ####################
#This Is an example of using getopts in Bash. It also contains some
#other bits of code find useful.
#Author: Linerd
#Website: Http://tuxtweaks.com/
#Copyright 2014
#License: Creative Commons Attribution-ShareAlike 4.0
#http: //creativecommons.org/licenses/by-sa/4.0/legalcode
################################################## ####################
#Set Script Name variable
SCRIPT = `basename $ {BASH_SOURCE [0]}`
#Initialize Variables to default values.
OPT_A = A
OPT_B = B
OPT_C = C
OPT_D = D
#Set Fonts for Help. [Annotation: Here tput terminal to change text attributes such as bold, highlight, etc.]
NORM = `tput sgr0`
BOLD = `tput bold`
REV = `tput smso`
#Help Function
function HELP {
echo -e \\ n "Help documentation for $ {BOLD} $ {SCRIPT}. $ {NORM}" \\ n
echo -e "$ {REV} Basic usage: $ {NORM} $ {BOLD} $ SCRIPT file.ext $ {NORM}" \\ n
echo "Command line switches are optional. The following switches are recognized."
echo "$ {REV} -a $ {NORM} --Sets the value for option $ {BOLD} a $ {NORM}. Default is $ {BOLD} A $ {NORM}."
echo "$ {REV} -b $ {NORM} --Sets the value for option $ {BOLD} b $ {NORM}. Default is $ {BOLD} B $ {NORM}."
echo "$ {REV} -c $ {NORM} --Sets the value for option $ {BOLD} c $ {NORM}. Default is $ {BOLD} C $ {NORM}."
echo "$ {REV} -d $ {NORM} --Sets the value for option $ {BOLD} d $ {NORM}. Default is $ {BOLD} D $ {NORM}."
echo -e "$ {REV} -h $ {NORM} --Displays this help message. No further functions are performed." \\ n
echo -e "Example: $ {BOLD} $ SCRIPT -a foo -b man -c chu -d bar file.ext $ {NORM}" \\ n
exit1
}
#Check The number of arguments. If none are passed, print help and exit.
NUMARGS = $ #
echo -e \\ n "Number of arguments: $ NUMARGS"
if [$ NUMARGS -eq 0]; then
HELP
fi
### Start getopts code ###
#Parse Command line flags
# If the option is followed by the parameters required, after the option plus ":"
# Caution "-h" option is not behind ":" because he does not need parameters. Option string beginning ":" is used to remove the error from getopts itself, but get unrecognized option. (Translation: If the option is not in the string ":" at the beginning, error (invalid option or missing parameter), getopts will print an error message to the error output; if the ":" at the beginning, it will not print [the man in called slient error reporting], while the option to assign OPTARG error variable)
while getopts: a: b: c: d: h FLAG; do
case $ FLAG in
a) #set option "a"
OPT_A = $ OPTARG
echo "-a used: $ OPTARG"
echo "OPT_A = $ OPT_A"
;;
b) #set option "b"
OPT_B = $ OPTARG
echo "-b used: $ OPTARG"
echo "OPT_B = $ OPT_B"
;;
c) #set option "c"
OPT_C = $ OPTARG
echo "-c used: $ OPTARG"
echo "OPT_C = $ OPT_C"
;;
d) #set option "d"
OPT_D = $ OPTARG
echo "-d used: $ OPTARG"
echo "OPT_D = $ OPT_D"
;;
h) #show help
HELP
;;
? \) # Unrecognized option - show help
echo -e \\ n "Option - $ {BOLD} $ OPTARG $ {NORM} not allowed."
HELP
# Here if you want to print the complete help information, you want to show a simple error message, remove the two lines above, use the following two lines.
#echo -e "Use $ {BOLD} $ SCRIPT -h $ {NORM} to see the help documentation." \\ n
#exit 2
;;
esac
done
shift $ ((OPTIND-1)) # This tells getopts to move on to the next argument.
### End getopts code ###
### Main loop to process files ###
# Where you can use your script processing logic instead. This example just prints the file name and file extension in the terminal. You can put any other document processing tasks into the while-do loop.
while [$ # -ne 0]; do
FILE = $ 1
TEMPFILE = `basename $ FILE`
#TEMPFILE = "$ {FILE ## * /}" # another method to obtain the file name without the suffix.
FILE_BASE = `echo" $ {TEMPFILE%. *} "` #file Without extension
FILE_EXT = "$ {TEMPFILE ## *.}" # File extension
echo -e \\ n "Input file is: $ FILE"
echo "File withouth extension is: $ FILE_BASE"
echo -e "File extension is: $ FILE_EXT" \\ n
shift #Move on to next input file.
done
### End main loop ###
exit0
Copy the above code into your text editor, and then save it to your executable path. I named this script options and save it to / home / linerd / bin path. Remember to add your script executable permissions after saving.

chmod + x ~ / bin / options
Now the script has run. Try using -h parameter to print help it.

options -h
Encountered an unsupported option, the script can also be given prompt and prints help information.

options -z
Finally, getopts can be in any order you to handle command-line arguments. The only restriction is that you have to deal with file must be placed at the end of all parameters.

options -d bar -c chu -b man -a foo example1.txt example2.txt
Now you can see from these examples of how the command-line arguments to the script in the variable. This script getopts in addition there are many other things, but I think these is enough to become the beginning of a new template of the script. If you are interested in more in-depth study of bash getopts, you can look deep in the man page of "Builtins" section in this document can also find information from the Bash Reference Manual.
     
         
         
         
  More:      
 
- Common DDOS attacks (Linux)
- YUM install desktop environment in CentOS (Linux)
- Detailed Linux platform chip programming process (Programming)
- How x2g0 install Remote Desktop on Linux VPS (Server)
- To configure and use multiple Tomcat under Linux environment (Server)
- Linux Operating System Security Management describes the top ten tricks (Linux)
- pkg-config to use (Linux)
- Oracle ordinary users show parameter method (Database)
- About Leetcode on Binary Tree Algorithm summary (Programming)
- Ubuntu users Steam controller does not work solutions (Linux)
- Linux Systemd-- To start / stop / restart services in RHEL / CentOS 7 (Linux)
- Hadoop vs spark (Server)
- How to Upgrade Ubuntu GNOME 14.10 to GNOME 3.16 Desktop (Linux)
- Ubuntu under shadowsocks configuration instructions (Linux)
- Linux folder and extract the differential file comparison techniques -rsync Magical (Linux)
- Linux 101 hack book reading notes (Linux)
- Smack 4.1.x Upgrade Guide (Linux)
- MySQL Error Code Complete (Database)
- Distributed Firewall Design on Linux platform (Linux)
- Analysis of memory mapping process in Linux x86-64 mode (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.