Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

bash parse arguments

#!/bin/bash

SHORT=c:,d:,h
LONG=city1:,city2:,help
OPTS=$(getopt --alternative --name weather --options $SHORT --longoptions $LONG -- "$@")
Comment

bash argument parsing

cat >/tmp/demo-space-separated.sh <<'EOF'
#!/bin/bash

POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -e|--extension)
    EXTENSION="$2"
    shift # past argument
    shift # past value
    ;;
    -s|--searchpath)
    SEARCHPATH="$2"
    shift # past argument
    shift # past value
    ;;
    -l|--lib)
    LIBPATH="$2"
    shift # past argument
    shift # past value
    ;;
    --default)
    DEFAULT=YES
    shift # past argument
    ;;
    *)    # unknown option
    POSITIONAL+=("$1") # save it in an array for later
    shift # past argument
    ;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

echo "FILE EXTENSION  = ${EXTENSION}"
echo "SEARCH PATH     = ${SEARCHPATH}"
echo "LIBRARY PATH    = ${LIBPATH}"
echo "DEFAULT         = ${DEFAULT}"
echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l)
if [[ -n $1 ]]; then
    echo "Last line of file specified as non-opt/last argument:"
    tail -1 "$1"
fi
EOF

chmod +x /tmp/demo-space-separated.sh

/tmp/demo-space-separated.sh -e conf -s /etc -l /usr/lib /etc/hosts
Comment

shell parse command line arguments

cat >/tmp/demo-space-separated.sh <<'EOF'
#!/bin/bash

POSITIONAL_ARGS=()

while [[ $# -gt 0 ]]; do
  case $1 in
    -e|--extension)
      EXTENSION="$2"
      shift # past argument
      shift # past value
      ;;
    -s|--searchpath)
      SEARCHPATH="$2"
      shift # past argument
      shift # past value
      ;;
    --default)
      DEFAULT=YES
      shift # past argument
      ;;
    -*|--*)
      echo "Unknown option $1"
      exit 1
      ;;
    *)
      POSITIONAL_ARGS+=("$1") # save positional arg
      shift # past argument
      ;;
  esac
done

set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters

echo "FILE EXTENSION  = ${EXTENSION}"
echo "SEARCH PATH     = ${SEARCHPATH}"
echo "DEFAULT         = ${DEFAULT}"
echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l)

if [[ -n $1 ]]; then
    echo "Last line of file specified as non-opt/last argument:"
    tail -1 "$1"
fi
EOF

chmod +x /tmp/demo-space-separated.sh

/tmp/demo-space-separated.sh -e conf -s /etc /etc/hosts
Comment

PREVIOUS NEXT
Code Example
Shell :: Missing essential plugins: com.android.tools.design org.jetbrains.android 
Shell :: zsh in Mac OS 
Shell :: powershell how to delete files or folders last updated older than x days 
Shell :: clone a repository 
Shell :: how to delete branch git cli 
Shell :: install git on windows 10 
Shell :: REMOTE HOST IDENTIFICATION HAS CHANGED! how to fix in ubuntu 
Shell :: how to move many folders linux 
Shell :: install brew linux 
Shell :: Does Ubuntu use deb or rpm? 
Shell :: Invalid base64 sqs 
Shell :: unporotected ssh key aws 
Shell :: install heroku cli 
Shell :: read a file and count how many lines 
Shell :: docker run with privileges 
Shell :: grep last instance 
Shell :: download spyder without anaconda 
Shell :: Managing the Nginx Process 
Shell :: how to undo local commit git 
Shell :: git checkout all deleted files 
Shell :: sound output raspberry pi 
Shell :: git create branch from commit 
Shell :: stop avd 
Shell :: generate jks certificate 
Shell :: linux iso dd 
Shell :: linux change user and group 
Shell :: create default package.json 
Shell :: how to change webmin password from command line 
Shell :: print variable in bash 
Shell :: zip directory in linux 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =