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 :: bash echo each line 
Shell :: docker get in python container 
Shell :: gitkraken stuck on opening repo 
Shell :: basename linux command 
Shell :: how to install raspap 
Shell :: rman commands 
Shell :: git lfs 
Shell :: create new branch 
Shell :: brew Cannot install on Intel processor in ARM default prefix (/opt/homebrew)! 
Shell :: print next 10 lines after grep 
Shell :: terminal archive files 
Shell :: command to find out my git head version 
Shell :: copy file from ssh to local windows 
Shell :: shutdown shortcut ubuntu 
Shell :: git discard changes 
Shell :: scp copy directories 
Shell :: Update /etc/apt/sources.list file 
Shell :: how to get specific lines of shell output 
Shell :: git stash to checkout master 
Shell :: quit nano 
Shell :: setup ssh key 
Shell :: docker run multiple commands in dockerfile 
Shell :: git bring back deleted branch from local 
Shell :: run cron job as specific user 
Shell :: Move or change directory to specific location in Terminal 
Shell :: close vim 
Shell :: bash script create file 
Shell :: flutter ui upload multiple image 
Shell :: which command is used to create a new git repository 
Shell :: how to install path adb 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =