-1

what I tried.

#!/bin/bash
# set -e

echo hello while getopts sr o; do case $o in s) echo "no" yell() { echo "$0: $" >&2; } die() { yell "$"; exit 111; } try() { "$@" || die "cannot $*"; }

  ;;
r) 
    echo "yes"
    yell() { echo "$0: $*" >&2; }
    die() { yell "$*"; exit 111; }
    try() { "$@" || die "cannot $*"; }
    ;;
\?) exit 3;; #invalid option

esac done

echo after_case

What I am trying to do: in terminal, the following 2 command: bash /home/jian/Desktop/pgdev/test1.sh -s
bash /home/jian/Desktop/pgdev/test1.sh -r
do not print out aftercase.

references: How to use getopts in bash
https://stackoverflow.com/questions/1378274/in-a-bash-script-how-can-i-exit-the-entire-script-if-a-certain-condition-occurs

jian
  • 567
  • 1
    As written the program loops over the provided options. A case statement processes each option, doing an echo and defining 3 functions. When there are no more options then loop exists and then the program prints out "after_case". To avoid getting to the after_case you could call exit in the case statement. However if you do so there is no point in defining the functions as the definitions will be lost when the shell exits. Normally you would put the main part of your script where it says after_case, using the yell die try statements. What are you actually trying to do? – icarus Oct 12 '23 at 19:20
  • Why do you expect the behaviour you describe? There is nothing that actually stop the script when only given r and/or s as options. – Henrik supports the community Oct 12 '23 at 21:30
  • what I am trying to do: based on getopt, stop executing in the middle of a script file. @icarus – jian Oct 13 '23 at 02:14
  • 1
    Why are you defining the yell/die/try functions multiple times? They don't seem to change, so just put the once at the top of the script. Or do you think that defining the try function like try() { "$@" || die "cannot $*"; } actually also invokes it? – muru Oct 13 '23 at 02:28
  • @muru because I don't understand. So I give It a try. I thought yell/die/try a least one will exit the script. – jian Oct 13 '23 at 02:36
  • Defining a function doesn't call it. You need to actually use try or die as a command. – muru Oct 13 '23 at 02:39
  • @muru. not sure what you mean. basically. sometimes when I do development, I need bash script to stop in the middle of a script file. Executing the whole script would take long. sometimes I only want part a of a bash file. – jian Oct 13 '23 at 02:43
  • 1
    func() { ... } is a function definition, it creates the function. It doesn't run it. The command to exit the script is exit, the same you have there in the last branch of the case. – ilkkachu Oct 13 '23 at 05:29
  • yes! @ilkkachu that's the answer!!! – jian Oct 13 '23 at 06:11

1 Answers1

0

You can do something like this. Your yell(), die() and try() function can be written at the top of the script.

#!/usr/bin/env bash
#
usage() {
    echo "usage: <command> options:<s|r|o>"
    echo "-s s_value -r r_value -o o_value"
}
yell() { echo "$0: $*" >&2; }
die() { yell "$*"; exit 111; }
try() { "$@" || die "cannot $*"; }
no_args="true"
while getopts s:r:o: flag; do
    case "${flag}" in
    s) yell
       die
       try
          ;;
    r) resource=${OPTARG} ;;
    o) other=${OPTARG} ;;
    *)
        usage
        exit
        ;;
    esac
    no_args="false"
done
[[ "$no_args" == "true" ]] && {
    usage
    exit 1
}
## other things to do

  • Why the yell, die and try calls in s)? The last one doesn't even do anything – ilkkachu Oct 13 '23 at 05:31
  • you can do something like s) some=${OPTARG} ;; and invoke this in another line. the original question was like that, thats why i mentioned it – Komkart Oct 13 '23 at 06:32
  • original question was just to exit. If you look at how the functions are defined, you'll note that yell doesn't do anything useful much without arguments, and die exits the script so the try after it doesn't get to do anything – ilkkachu Oct 13 '23 at 06:40