How do I parse command line arguments in Bash?

Say, I have a script that gets called with this line: ./myscript -vfd ./foo/bar/someFile -o /fizz/someOtherFile or this one: ./myscript -v -f -d -o /fizz/someOtherFile ./foo/bar/someFile...

Is there a replacement for unistd.h for Windows (Visual C)?

I'm porting a relatively simple console program written for Unix to the Windows platform (Visual C++ 8.0). All the source files include "unistd.h", which doesn't exist. Removing it, I get...

Using getopts to process long and short command line options

I wish to have long and short forms of command line options invoked using my shell script. I know that getopts can be used, but like in Perl, I have not been able to do the same with shell. Any...

getopt does not parse optional arguments to parameters

In C, getopt_long does not parse the optional arguments to command line parameters parameters. When I run the program, the optional argument is not recognized like the example run below. $...

getopt() not enforcing required arguments?

I'm having problems with this getopt() code in a script that I'm writing which does some simple file manipulation given 2 required parameters (input filename and output filename) and/or 2...

Perl GetOptions() case sensitivity

GetOptions( "r|repo=s" => \$repo, "R|list-repos" => \$list, ); When I call this script with -r qwe option, $list is updated to 1, which is not what I expect. How can I make...

getopt value stays null

I am passing my program inputs and I could see them in argv but getopt doesnt seem to have the argument that I expect. This is how I run my prog: ./my_prog -X -f filename <snip> while ((opt =...

Open filehandle or assign stdout

I'm working in a program where the user can pass a -o file option, and output should be then directed to that file. Otherwise, it should go to stdout. To retrieve the option I'm using the module...

Functions vs Scripts - Unix Bash Programming

I am fairly new to programming (about one year - with java) and extremely new to bash (about six weeks). I wrote a small application that does a few things, and it consits of entirely scripts. Not...

Retrieving multiple arguments for a single option using getopts in Bash

I need help with getopts. I created a Bash script which looks like this when run: $ foo.sh -i env -d directory -s subdirectory -f file It works correctly when handling one argument from each flag....

Optional option argument with getopts

while getopts "hd:R:" arg; do case $arg in h) echo "usgae" ;; d) dir=$OPTARG ;; R) if [[ $OPTARG =~ ^[0-9]+$ ]];then level=$OPTARG else ...

Is it possible to repeat getopt

I'm trying to create a basic shell with builtin commands, and I'm having some issues with getopt. Here is the output (using valgrind): $ mkdir -p foo/bar mkdir -p foo/bar FLAGON $ mkdir -p...

An example of how to use getopts in bash

I want to call myscript file in this way: $ ./myscript -s 45 -p any_string or $ ./myscript -h #should display help $ ./myscript #should display help My requirements are: getopt here to...

Getopt- Passing string parameter for argument

I have a program which takes in multiple command line arguments so I am using getopt. One of my arguments takes in a string as a parameter. Is there anyway to obtain that string through the getopt...

Using getopt in C with non-option arguments

I'm making a small program in C that deals with a lot of command line arguments, so I decided to use getopt to sort them for me. However, I want two non-option arguments (source and destination...

Linux C getopt ignore after the first unspecified argument

I wanted to make the program accomplish "execvp(argv[1],argv+1);" if the first argument is undefined, but getopt parsing all arguments starting with "-". How to ignore all the arguments after the...

bash getopts multiple arguments or default value

So I have a question about get opts in bash. I want to get the value of the arguments if they are present but if they are not present to use a default value. So the script should take a...

Using getopts to read one optional parameter placed as final place

I wrote a bash script that takes flexible number of parameters and now I would like to add an optional argument (-l) to each of them. I am currently having difficulty getting the desired...

How to pair Fastq files after FASTQC trimming?

I used the below script to pair it but it was giving error. Can anyone help me in pairing my files? my file look like...

Passing Negative Numbers into Getopt

I need to pass negative numbers into getopt and I was wondering if there is a simple way to change the prefix used by getopt (i.e. '-' character to flag in the case statement) into a different...

How to use ${OPTARG} on getopts?

I have the following code: while getopts ":p:t:n:" o; do case "${o}" in p) p=${OPTARG} numep=$p mkdir $numep ;; t) t=${OPTARG} tip=$t if [ $tip == "c" ];...

The variable optarg of the function getopt()

I've read through the documentation for the function getopt(), but I do not find the explanations clear, especially with regards to the variable optarg. I cannot find any other sources that...

Why does getopts only work the first time?

Why does this option only work the first time it's used, then ignored every other time? It's like it's being reset when the option is not used. Here's my function: testopts() { local var="o...

Handling unused getopts argument

I have a script that starts with getopts and looks as follows: USAGE() { echo -e "Usage: bash $0 [-w <in-dir>] [-o <out-dir>] [-c <template1>] [-t <template2>] \n" 1>&2; exit 1; } if (($# ==...

how to install python modules sys, os and getopt on unbuntu?

My python version is Python 2.7.17 I usually use pip to install other modules like shutil and itertool etc. I can't find the package name for os, getopt and sys modules. Any help will be...

Bash : Parse options after arguments with getopts

In a script which request some arguments (arg) and options (-a), I would like to let the script user the possibility to place the options where he wants in the command line. Here is my code...

make directory with bash builtin

I am writing bash script for Windows 7 and have limitation to bash.exe mkdir -p does not work (command not found) I am looking for any replacement/cheat with bash builtins. (the -p flag is not...

How to identify likely broken pdf pages before extracting its text?

TL;DR My workflow: Download PDF Split it into pages using pdftk Extract text of each page using pdftotext Classify text and add metadata Send it to client in a structured format I need to...

Building Glibc 2.33 for Linux results in unit test failures

I'm currently trying to create a build of Glibc 2.33 to live in parallel to the default system version, but the build process is resulting in a large number of unit test failures. While some of...

"parse error near `|'" when sourcing script file in zsh

I'm using zsh (5.4.2) with oh-my-zsh as my shell on Ubuntu 18.04. The following script is executed correctly in zsh when I just run it (./my_script -a 1): #! /bin/bash while getopts "a:"...

Relevant tags