Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify command line arguments like name=value pairs for shell script

Tags:

shell

tcsh

Is it possible to pass command line arguments to shell script as name value pairs, something like

myscript action=build module=core

and then in my script, get the variable like $action and process it?

I know that $1....and so on can be used to get variables, but then won't be name value like pairs. Even if they are, then the developer using the script will have to take care of declaring variables in the same order. I do not want that.

like image 547
Neeraj Avatar asked Mar 31 '11 11:03

Neeraj


People also ask

How do you assign command-line arguments in shell script?

Pass arguments from command line - shell script To pass arguments from the command line, add the argument values after the file name while executing the script. In script $1 will refer the value of agr1, and $2 will refer the value of arg2 and so on.

What are command-line arguments in shell script?

Command-line arguments are parameters that are passed to a script while executing them in the bash shell. They are also known as positional parameters in Linux. We use command-line arguments to denote the position in memory where the command and it's associated parameters are stored.

What is $@ and $* in shell script?

• $* - It stores complete set of positional parameter in a single string. • $@ - Quoted string treated as separate arguments. • $? - exit status of command.

What is $? == 0 in shell script?

$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded. The grep manpage states: The exit status is 0 if selected lines are found, and 1 if not found.


2 Answers

This worked for me:

for ARGUMENT in "$@"
do
   KEY=$(echo $ARGUMENT | cut -f1 -d=)

   KEY_LENGTH=${#KEY}
   VALUE="${ARGUMENT:$KEY_LENGTH+1}"

   export "$KEY"="$VALUE"
done

echo "STEPS = $STEPS"
echo "REPOSITORY_NAME = $REPOSITORY_NAME"

Usage

bash my_scripts.sh  STEPS="ABC" REPOSITORY_NAME="stackexchange"

Console result :

STEPS = ABC
REPOSITORY_NAME = stackexchange

STEPS and REPOSITORY_NAME are ready to use in the script.

It does not matter what order the arguments are in.

HTH

like image 95
JRichardsz Avatar answered Sep 21 '22 11:09

JRichardsz


It's quite an old question, but still valid I have not found the cookie cut solution. I combined the above answers. For my needs I created this solution; this works even with white space in the argument's value.

Save this as argparse.sh

#!/bin/bash

: ${1?
  'Usage:
  $0 --<key1>="<val1a> <val1b>" [ --<key2>="<val2a> <val2b>" | --<key3>="<val3>" ]'
}

declare -A args
while [[ "$#" > "0" ]]; do
  case "$1" in 
    (*=*)
        _key="${1%%=*}" &&  _key="${_key/--/}" && _val="${1#*=}"
        args[${_key}]="${_val}"
        (>&2 echo -e "key:val => ${_key}:${_val}")
        ;;
  esac
  shift
done
(>&2 echo -e "Total args: ${#args[@]}; Options: ${args[@]}")

## This additional can check for specific key
[[ -n "${args['path']+1}" ]] && (>&2 echo -e "key: 'path' exists") || (>&2 echo -e "key: 'path' does NOT exists");

@Example: Note, arguments to the script can have optional prefix --

./argparse.sh --x="blah"
./argparse.sh --x="blah" --yy="qwert bye"
./argparse.sh x="blah" yy="qwert bye"

Some interesting use cases for this script:

./argparse.sh --path="$(ls -1)"
./argparse.sh --path="$(ls -d -1 "$PWD"/**)"

Above script created as gist, Refer: argparse.sh

like image 40
mangalbhaskar Avatar answered Sep 22 '22 11:09

mangalbhaskar