• exit in script

    From vjp2.at@at.BioStrategist.dot.dot.co@21:1/5 to All on Tue Dec 7 06:27:42 2021
    I want to make a proc that does

    run first
    echo enough
    read nuff
    if nuff then exit
    else second
    exhout enough
    read nuff
    if nuff then exit
    else third

    I'm unclear about the if/exit syntax


    --
    Vasos Panagiotopoulos panix.com/~vjp2/vasos.htm
    ---{Nothing herein constitutes advice. Everything fully disclaimed.}---

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to vjp2.at@at.BioStrategist.dot.dot.co on Tue Dec 7 08:16:59 2021
    On 07.12.2021 07:27, vjp2.at@at.BioStrategist.dot.dot.com wrote:
    I want to make a proc that does

    run first
    echo enough
    read nuff
    if nuff then exit
    else second
    exhout enough
    read nuff
    if nuff then exit
    else third

    I'm unclear about the if/exit syntax

    Assuming you are using a modern shell (ksh, bash, zsh),
    assuming 'first' and 'second' and 'third' are commands or programs,
    assuming by "if nuff" you want to test against non-empty input,
    and I left out (commented out) the exhout thing (no idea what you mean), finally you may (or may not) want to provide an exit code to 'exit'
    ('exit 0' if successful, 'exit 1' to return an error indication.

    first
    echo enough
    read nuff
    if [[ -n "$nuff" ]]
    then exit
    else second
    fi
    # exhout enough
    read nuff
    if [[ -n "$nuff" ]]
    then exit
    else third
    fi


    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to vjp2.at@at.BioStrategist.dot.dot.co on Wed Dec 8 07:00:12 2021
    On 08.12.2021 06:34, vjp2.at@at.BioStrategist.dot.dot.com wrote:
    In <son1pb$qe5$1@dont-email.me> by Janis Papanagnou <janis_papanagnou@hotmail.com> on Tue, 07 Dec 2021 02:16:59 we perused:
    *+-On 07.12.2021 07:27, vjp2.at@at.BioStrategist.dot.dot.com wrote:

    I'm unclear about the if/exit syntax

    *+-finally you may (or may not) want to provide an exit code to 'exit' *+-('exit 0' if successful, 'exit 1' to return an error indication.

    Thanks much! The numbers after the exit confused me

    'exit' is a command that exits the script and passes a return code
    to the caller so that the caller can determine the processing state
    (like success or various fail situations) of the called script.

    A value of 0 indicates success, and values >0 indicate some sort of
    error, or warning, or other state necessary/sensible in your context.

    Say, you have a script "any_args" defined as

    if [[ $# != 0 ]] ; then exit 0 ; else exit 1 ; fi

    then you can call it like that

    any_args
    or
    any_args A B C

    and get a failure code in the first case and a success indication in
    the second case. So you can embed that in code, e.g.

    if any_args ${unknown_content}
    then echo arguments provided
    else echo no arguments
    fi

    Error and status handling is crucial in programming, also in shell.

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to vjp2.at@at.BioStrategist.dot.dot.co on Wed Dec 8 07:07:25 2021
    On 08.12.2021 06:36, vjp2.at@at.BioStrategist.dot.dot.com wrote:
    Basically I want to run the same search through three search engines
    (brave, qwant, dogpile) but if I'm happy witht he first round,
    stop searching.

    Then another logic, a loop, might be preferable. Something like

    for search_engine in brave qwant dogpile
    do
    "$search_engine" whatever arguments
    echo "continue? (y/n)" ; read yn
    [[ "$yn" != "y" ]] && exit 0
    done
    echo "nothing appropriate found"
    exit 1


    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From vjp2.at@at.BioStrategist.dot.dot.co@21:1/5 to All on Wed Dec 8 06:02:23 2021
    Thanks, I got it, worked on first try!! :

    wearch () {
    SRCH=`echo $* | sed -e 's/ /+/g'`;
    lynx -accept_all_cookies -useragent 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0' 'https://search.brave.com/search?q='$SRCH ;
    echo "return if enough else qwant";
    read nuff;
    if [[ -n "$nuff" ]]
    then exit
    else lynx -accept_all_cookies -useragent 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0' 'https://www.qwant.com/?q='$SRCH ;
    fi;
    echo "return if enough else dogpile";
    read nuff;
    if [[ -n "$nuff" ]]
    then exit
    else lynx -accept_all_cookies -useragent 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0' 'http://www.dogpile.com/search/web?q='$SRCH ;
    fi;
    }


    --
    Vasos Panagiotopoulos panix.com/~vjp2/vasos.htm
    ---{Nothing herein constitutes advice. Everything fully disclaimed.}---

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From vjp2.at@at.BioStrategist.dot.dot.co@21:1/5 to All on Wed Dec 8 05:34:14 2021
    In <son1pb$qe5$1@dont-email.me> by Janis Papanagnou <janis_papanagnou@hotmail.com> on Tue, 07 Dec 2021 02:16:59 we perused:
    *+-On 07.12.2021 07:27, vjp2.at@at.BioStrategist.dot.dot.com wrote:
    I want to make a proc that does

    run first
    echo enough
    read nuff
    if nuff then exit
    else second
    exhout enough
    read nuff
    if nuff then exit
    else third

    I'm unclear about the if/exit syntax

    *+-Assuming you are using a modern shell (ksh, bash, zsh),
    *+-assuming 'first' and 'second' and 'third' are commands or programs, *+-assuming by "if nuff" you want to test against non-empty input,
    *+-and I left out (commented out) the exhout thing (no idea what you mean), *+-finally you may (or may not) want to provide an exit code to 'exit' *+-('exit 0' if successful, 'exit 1' to return an error indication.

    *+- first
    *+- echo enough
    *+- read nuff
    *+- if [[ -n "$nuff" ]]
    *+- then exit
    *+- else second
    *+- fi
    *+- # exhout enough
    *+- read nuff
    *+- if [[ -n "$nuff" ]]
    *+- then exit
    *+- else third
    *+- fi


    *+-Janis

    Thanks much! The numbers after the exit confused me



    --
    Vasos Panagiotopoulos panix.com/~vjp2/vasos.htm
    ---{Nothing herein constitutes advice. Everything fully disclaimed.}---

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From vjp2.at@at.BioStrategist.dot.dot.co@21:1/5 to All on Wed Dec 8 05:36:26 2021
    Basically I want to run the same search through three search engines
    (brave, qwant, dogpile) but if I'm happy witht he first round,
    stop searching.

    --
    Vasos Panagiotopoulos panix.com/~vjp2/vasos.htm
    ---{Nothing herein constitutes advice. Everything fully disclaimed.}---

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From vjp2.at@at.BioStrategist.dot.dot.co@21:1/5 to All on Thu Dec 9 05:46:39 2021
    Not really, but I think the second if should be an elif
    and exit lands me back in my interrupted login script
    which makes me wonder if it might not also log me off.
    I changed the -n to -z because "no" exited

    --
    Vasos Panagiotopoulos panix.com/~vjp2/vasos.htm
    ---{Nothing herein constitutes advice. Everything fully disclaimed.}---

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From vjp2.at@at.BioStrategist.dot.dot.co@21:1/5 to All on Thu Dec 9 05:52:24 2021
    exit does indeed log me out

    yikes

    foolish of me to think it would work on the first try


    --
    Vasos Panagiotopoulos panix.com/~vjp2/vasos.htm
    ---{Nothing herein constitutes advice. Everything fully disclaimed.}---

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to vjp2.at@at.BioStrategist.dot.dot.co on Thu Dec 9 07:52:27 2021
    On 09.12.2021 06:46, vjp2.at@at.BioStrategist.dot.dot.com wrote:
    Not really, but I think the second if should be an elif
    and exit lands me back in my interrupted login script
    which makes me wonder if it might not also log me off.
    I changed the -n to -z because "no" exited


    This is Usenet. Without posted context your post is usually
    incomprehensible.

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to vjp2.at@at.BioStrategist.dot.dot.co on Thu Dec 9 07:54:16 2021
    On 09.12.2021 06:52, vjp2.at@at.BioStrategist.dot.dot.com wrote:
    exit does indeed log me out

    'exit' terminates the program that contains that command.
    If used interactively from a shell session it will exit the shell.

    If you want to return from a function use 'return'.

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to vjp2.at@at.BioStrategist.dot.dot.co on Thu Dec 9 08:16:09 2021
    On 08.12.2021 07:02, vjp2.at@at.BioStrategist.dot.dot.com wrote:
    Thanks, I got it, worked on first try!! :

    wearch () {
    SRCH=`echo $* | sed -e 's/ /+/g'`;
    lynx -accept_all_cookies -useragent 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0' 'https://search.brave.com/search?q='$SRCH ;
    echo "return if enough else qwant";
    read nuff;
    if [[ -n "$nuff" ]]
    then exit
    else lynx -accept_all_cookies -useragent 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0' 'https://www.qwant.com/?q='$SRCH ;
    fi;
    echo "return if enough else dogpile";
    read nuff;
    if [[ -n "$nuff" ]]
    then exit
    else lynx -accept_all_cookies -useragent 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0' 'http://www.dogpile.com/search/web?q='$SRCH ;
    fi;
    }

    Based on the code above and some comments dispersed in the thread
    you may want to have a look at the subsequent code with changes

    * substitution is done by shell (without sed)
    * browser ID is extracted in one place
    * a loop is used (simply extensible for more search engines)
    * exit is replaced by return
    * search will only be continued with next engine if 'y' is typed
    * if no search reult are not accepted by user then status 1 is returned
    * otherwise status 0
    * the # commented line needs to be uncommented to operate
    * the code requires a modern shell to run like ksh, bash, zsh

    Hope that helps.

    Janis


    wearch()
    {
    ARGS=$*
    SRCH=${ARGS// /+}
    id='Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0'
    for search_engine in \
    'https://search.brave.com/search' \
    'https://www.qwant.com/' \
    'http://www.dogpile.com/search/web'
    do
    echo "do search with $search_engine"
    # lynx -accept_all_cookies -useragent "$id" "$search_engine?q=$SRCH"
    echo "continue? (y/n)" ; read yn
    [[ "$yn" != "y" ]] && return 0
    done
    echo "nothing appropriate found"
    return 1
    }

    wearch Janis Joplin
    echo search returned state $?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From vjp2.at@at.BioStrategist.dot.dot.co@21:1/5 to All on Fri Dec 10 04:57:42 2021
    Thanks

    --
    Vasos Panagiotopoulos panix.com/~vjp2/vasos.htm
    ---{Nothing herein constitutes advice. Everything fully disclaimed.}---

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Geoff Clare@21:1/5 to Janis Papanagnou on Fri Dec 10 13:41:05 2021
    Janis Papanagnou wrote:

    On 08.12.2021 07:02, vjp2.at@at.BioStrategist.dot.dot.com wrote:

    wearch () {
    SRCH=`echo $* | sed -e 's/ /+/g'`;

    Based on the code above and some comments dispersed in the thread
    you may want to have a look at the subsequent code with changes

    * substitution is done by shell (without sed)

    wearch()
    {
    ARGS=$*
    SRCH=${ARGS// /+}

    This change introduces a dependency on the value of IFS. It will
    only work correctly if IFS is not empty and the first character is a
    space (which it is by default, but IFS could be changed by code that
    calls the wearch function).

    My inclination would be to use a loop to build the SRCH value:

    SRCH=""
    for ARG
    do
    SRCH="$SRCH${SRCH:++}$ARG"
    done

    This, together with changing [[ "$yn" != "y" ]] to [ "$yn" != "y" ],
    would make the function work with any POSIX shell.

    --
    Geoff Clare <netnews@gclare.org.uk>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Geoff Clare on Fri Dec 10 16:28:46 2021
    On 10.12.2021 14:41, Geoff Clare wrote:
    Janis Papanagnou wrote:

    On 08.12.2021 07:02, vjp2.at@at.BioStrategist.dot.dot.com wrote:

    wearch () {
    SRCH=`echo $* | sed -e 's/ /+/g'`;

    Based on the code above and some comments dispersed in the thread
    you may want to have a look at the subsequent code with changes

    * substitution is done by shell (without sed)

    wearch()
    {
    ARGS=$*
    SRCH=${ARGS// /+}

    This change introduces a dependency on the value of IFS. It will
    only work correctly if IFS is not empty and the first character is a
    space (which it is by default, but IFS could be changed by code that
    calls the wearch function).

    Interesting.

    This simple change seems to work, though, with IFS=''

    ARGS=$@
    SRCH=${ARGS// /+}

    (or please correct me if I am wrong).

    (But I am puzzled at the moment why

    ARGS=$@
    SRCH=${ARGS// */+}

    doesn't work. Guess I need some coffee.)


    My inclination would be to use a loop to build the SRCH value:

    SRCH=""
    for ARG
    do
    SRCH="$SRCH${SRCH:++}$ARG"
    done

    This, together with changing [[ "$yn" != "y" ]] to [ "$yn" != "y" ],
    would make the function work with any POSIX shell.

    Or even make the y/n decision complete

    case $yn in
    (y|Y) ... ;;
    (n|N) ... ;;
    (*) ... ;;
    esac

    (also POSIX conforming).

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Helmut Waitzmann@21:1/5 to All on Fri Dec 10 19:16:53 2021
    Janis Papanagnou <janis_papanagnou@hotmail.com>:

    (But I am puzzled at the moment why

    ARGS=$@
    SRCH=${ARGS// */+}

    doesn't work. Guess I need some coffee.)

    Perhaps because the pattern is a shell pattern rather than a regular expression?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Geoff Clare@21:1/5 to Janis Papanagnou on Mon Dec 13 13:20:44 2021
    Janis Papanagnou wrote:

    On 10.12.2021 14:41, Geoff Clare wrote:
    Janis Papanagnou wrote:

    On 08.12.2021 07:02, vjp2.at@at.BioStrategist.dot.dot.com wrote:

    wearch () {
    SRCH=`echo $* | sed -e 's/ /+/g'`;

    Based on the code above and some comments dispersed in the thread
    you may want to have a look at the subsequent code with changes

    * substitution is done by shell (without sed)

    wearch()
    {
    ARGS=$*
    SRCH=${ARGS// /+}

    This change introduces a dependency on the value of IFS. It will
    only work correctly if IFS is not empty and the first character is a
    space (which it is by default, but IFS could be changed by code that
    calls the wearch function).

    Interesting.

    This simple change seems to work, though, with IFS=''

    ARGS=$@
    SRCH=${ARGS// /+}

    (or please correct me if I am wrong).

    It only works with some shells. They differ in how they expand $@ in
    an assignment. (POSIX says the behaviour is unspecified.)

    $ bash -c 'set one two three; IFS=""; x=$@; echo "$x"'
    one two three
    $ ksh -c 'set one two three; IFS=""; x=$@; echo "$x"'
    one two three
    $ dash -c 'set one two three; IFS=""; x=$@; echo "$x"'
    onetwothree
    $ busybox sh -c 'set one two three; IFS=""; x=$@; echo "$x"'
    onetwothree

    --
    Geoff Clare <netnews@gclare.org.uk>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)