Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

exit in script

14 views
Skip to first unread message

vjp...@at.biostrategist.dot.dot.com

unread,
Dec 7, 2021, 1:29:04 AM12/7/21
to
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.}---

Janis Papanagnou

unread,
Dec 7, 2021, 2:17:04 AM12/7/21
to
On 07.12.2021 07:27, vjp...@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

vjp...@at.biostrategist.dot.dot.com

unread,
Dec 8, 2021, 12:34:20 AM12/8/21
to
In <son1pb$qe5$1...@dont-email.me> by Janis Papanagnou <janis_pa...@hotmail.com> on Tue, 07 Dec 2021 02:16:59 we perused:
*+-On 07.12.2021 07:27, vjp...@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

vjp...@at.biostrategist.dot.dot.com

unread,
Dec 8, 2021, 12:36:30 AM12/8/21
to
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.

Janis Papanagnou

unread,
Dec 8, 2021, 1:00:18 AM12/8/21
to
On 08.12.2021 06:34, vjp...@at.BioStrategist.dot.dot.com wrote:
> In <son1pb$qe5$1...@dont-email.me> by Janis Papanagnou <janis_pa...@hotmail.com> on Tue, 07 Dec 2021 02:16:59 we perused:
> *+-On 07.12.2021 07:27, vjp...@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

vjp...@at.biostrategist.dot.dot.com

unread,
Dec 8, 2021, 1:02:28 AM12/8/21
to
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;

Janis Papanagnou

unread,
Dec 8, 2021, 1:07:30 AM12/8/21
to
On 08.12.2021 06:36, vjp...@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

vjp...@at.biostrategist.dot.dot.com

unread,
Dec 9, 2021, 12:46:44 AM12/9/21
to
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

vjp...@at.biostrategist.dot.dot.com

unread,
Dec 9, 2021, 12:52:29 AM12/9/21
to
exit does indeed log me out

yikes

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

Janis Papanagnou

unread,
Dec 9, 2021, 1:52:32 AM12/9/21
to
On 09.12.2021 06:46, vjp...@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

Janis Papanagnou

unread,
Dec 9, 2021, 1:54:21 AM12/9/21
to
On 09.12.2021 06:52, vjp...@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

Janis Papanagnou

unread,
Dec 9, 2021, 2:16:15 AM12/9/21
to
On 08.12.2021 07:02, vjp...@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 $?

vjp...@at.biostrategist.dot.dot.com

unread,
Dec 9, 2021, 11:57:46 PM12/9/21
to
Thanks

Geoff Clare

unread,
Dec 10, 2021, 9:11:07 AM12/10/21
to
Janis Papanagnou wrote:

> On 08.12.2021 07:02, vjp...@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 <net...@gclare.org.uk>

Janis Papanagnou

unread,
Dec 10, 2021, 10:28:51 AM12/10/21
to
On 10.12.2021 14:41, Geoff Clare wrote:
> Janis Papanagnou wrote:
>
>> On 08.12.2021 07:02, vjp...@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

Helmut Waitzmann

unread,
Dec 10, 2021, 5:29:03 PM12/10/21
to
Janis Papanagnou <janis_pa...@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?

Geoff Clare

unread,
Dec 13, 2021, 8:41:07 AM12/13/21
to
Janis Papanagnou wrote:

> On 10.12.2021 14:41, Geoff Clare wrote:
>> Janis Papanagnou wrote:
>>
>>> On 08.12.2021 07:02, vjp...@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 <net...@gclare.org.uk>
0 new messages