Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #168773 > unrolled thread

bash help please

Started byGene Heskett <gheskett@shentel.net>
First post2016-06-10 04:50 +0200
Last post2016-06-10 09:10 +0200
Articles 11 — 5 participants

Back to article view | Back to linux.debian.user


Contents

  bash help please Gene Heskett <gheskett@shentel.net> - 2016-06-10 04:50 +0200
    Re: bash help please Lars Noodén <lars.nooden@gmail.com> - 2016-06-10 05:10 +0200
      Re: bash help please Gene Heskett <gheskett@shentel.net> - 2016-06-10 06:40 +0200
    Re: bash help please David Wright <deblis@lionunicorn.co.uk> - 2016-06-10 06:00 +0200
      Re: bash help please Gene Heskett <gheskett@shentel.net> - 2016-06-10 07:10 +0200
        Re: bash help please David Wright <deblis@lionunicorn.co.uk> - 2016-06-10 07:40 +0200
          Re: bash help please Gene Heskett <gheskett@shentel.net> - 2016-06-10 08:40 +0200
    Re: bash help please David Christensen <dpchrist@holgerdanske.com> - 2016-06-10 06:10 +0200
      Re: bash help please Gene Heskett <gheskett@shentel.net> - 2016-06-10 07:10 +0200
        Re: bash help please David Christensen <dpchrist@holgerdanske.com> - 2016-06-11 05:50 +0200
    Re: bash help please <tomas@tuxteam.de> - 2016-06-10 09:10 +0200

#168773 — bash help please

FromGene Heskett <gheskett@shentel.net>
Date2016-06-10 04:50 +0200
Subjectbash help please
Message-ID<rIkGd-74R-1@gated-at.bofh.it>
Greetings; 

A bash script that has worked most of a decade now refuses.

For instance, assume that var InMail is = "gene", and can be echoed from 
the command line using $InMail like this.
gene@coyote:~$ echo $InMail
gene
But I'll be switched if I can get a result from a line of code resembling 
this from the command line while attempting to troubleshoot a 110 line 
bash script: which asks "if test [${InMail} = "gene"]
			  then
				-----
			 elif (another name)
				yadda yadda

gene@coyote:~$ echo `test [${InMail} = "gene"]`

All I get is the linefeed.  Obviously I'm losing it, so how do I 
translate and get usefull output for troubleshooting?


Thanks bashers;

Cheers, Gene Heskett
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page <http://geneslinuxbox.net:6309/gene>

[toc] | [next] | [standalone]


#168774

FromLars Noodén <lars.nooden@gmail.com>
Date2016-06-10 05:10 +0200
Message-ID<rIkZz-7t5-5@gated-at.bofh.it>
In reply to#168773
On 06/10/2016 05:41 AM, Gene Heskett wrote:
> gene@coyote:~$ echo `test [${InMail} = "gene"]`
> 
> All I get is the linefeed.  Obviously I'm losing it, so how do I 
> translate and get usefull output for troubleshooting?

One option is to use 'set -x' there in the script.  It can go anywhere
above your problem and will show each line as it is executed.  The
contents of ${InMail} that are actually getting passed to test will be
visible.

Regards,
Lars

[toc] | [prev] | [next] | [standalone]


#168777

FromGene Heskett <gheskett@shentel.net>
Date2016-06-10 06:40 +0200
Message-ID<rImoG-8ma-13@gated-at.bofh.it>
In reply to#168774
On Thursday 09 June 2016 23:08:32 Lars Noodén wrote:

> On 06/10/2016 05:41 AM, Gene Heskett wrote:
> > gene@coyote:~$ echo `test [${InMail} = "gene"]`
> >
> > All I get is the linefeed.  Obviously I'm losing it, so how do I
> > translate and get usefull output for troubleshooting?
>
> One option is to use 'set -x' there in the script.  It can go anywhere
> above your problem and will show each line as it is executed.  The
> contents of ${InMail} that are actually getting passed to test will be
> visible.
>
> Regards,
> Lars

I am about to call up a copy of amrecover, and restore that script to one 
that was working last week, I cannot get by the syntax errors between fi 
& done in a while loop with a bunch of if-then-elif-fi's in it.  Last 
weeks wasn't perfect, but it did work, and everytime I look something up 
in a man bash & try to fix it, its worse. 

The set -x helped, and maybe I can use that to see what the original is 
doing.

So for now, thanks all.

Cheers, Gene Heskett
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page <http://geneslinuxbox.net:6309/gene>

[toc] | [prev] | [next] | [standalone]


#168775

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2016-06-10 06:00 +0200
Message-ID<rIlLX-7Pi-9@gated-at.bofh.it>
In reply to#168773
On Thu 09 Jun 2016 at 22:41:27 (-0400), Gene Heskett wrote:
> A bash script that has worked most of a decade now refuses.
> 
> For instance, assume that var InMail is = "gene", and can be echoed from 
> the command line using $InMail like this.
> gene@coyote:~$ echo $InMail
> gene
> But I'll be switched if I can get a result from a line of code resembling 
> this from the command line while attempting to troubleshoot a 110 line 
> bash script: which asks "if test [${InMail} = "gene"]
> 			  then
> 				-----
> 			 elif (another name)
> 				yadda yadda
> 
> gene@coyote:~$ echo `test [${InMail} = "gene"]`
> 
> All I get is the linefeed.  Obviously I'm losing it, so how do I 
> translate and get usefull output for troubleshooting?

$ `[ ${InMail} = "gene" ]` ; echo $?

0
$ `[ ${InMail} = "xgene" ]` ; echo $?
1
$ `test ${InMail} = "gene"` ; echo $?

0
$ `test ${InMail} = "xgene"` ; echo $?
1
$ 

elif needs [], not (), and it needs a then too.

[ is a command like test, so it must be followed by a space
before its argument.

Often the easiest way of solving these is grep -A ... used with
/etc/init.d/*

Cheers,
David.

[toc] | [prev] | [next] | [standalone]


#168780

FromGene Heskett <gheskett@shentel.net>
Date2016-06-10 07:10 +0200
Message-ID<rImRH-lD-11@gated-at.bofh.it>
In reply to#168775
On Thursday 09 June 2016 23:50:35 David Wright wrote:

> On Thu 09 Jun 2016 at 22:41:27 (-0400), Gene Heskett wrote:
> > A bash script that has worked most of a decade now refuses.
> >
> > For instance, assume that var InMail is = "gene", and can be echoed
> > from the command line using $InMail like this.
> > gene@coyote:~$ echo $InMail
> > gene
> > But I'll be switched if I can get a result from a line of code
> > resembling this from the command line while attempting to
> > troubleshoot a 110 line bash script: which asks "if test [${InMail}
> > = "gene"]
> > 			  then
> > 				-----
> > 			 elif (another name)
> > 				yadda yadda
> >
> > gene@coyote:~$ echo `test [${InMail} = "gene"]`
> >
> > All I get is the linefeed.  Obviously I'm losing it, so how do I
> > translate and get usefull output for troubleshooting?
>
> $ `[ ${InMail} = "gene" ]` ; echo $?
> 0
> $ `[ ${InMail} = "xgene" ]` ; echo $?
> 1
> $ `test ${InMail} = "gene"` ; echo $?
> 0
> $ `test ${InMail} = "xgene"` ; echo $?
> 1
> $
>
> elif needs [], not (), and it needs a then too.
>
> [ is a command like test, so it must be followed by a space
> before its argument.
>
> Often the easiest way of solving these is grep -A ... used with
> /etc/init.d/*

This script is home brew, and has never had a starter in /etc/init.d
Its normally something I start after the kmail gui is up and running.  
But restarting tdm-trinity apparently doesn't kill any background 
daemons which this normally runs as, and the excessive downtime of kmail 
let it stuff dcops buffer full, and then everything that needed dcop 
promptly fell over too, including cups.

The only recovery from that is a reboot which I did, but I screwed the 
pooch on this script before the reboot.  Big dummy at work of course.  
BTDT, 50 times or more over the last decade plus I've been doing this to 
offload the mail fetching from kmail as it locks up everything while its 
doing that, 5 minutes plus at a time back when  all this was done on 
dialup on a 2400 baud modem.  Thats possibly before some peoples time 
here on this list. :)

And I just amrecover'd a week old version that is working fairly well 
although its burning up the shell history file since I put a set -x in 
the top of it.  So I can at least go get some zz's.

> Cheers,
> David.

Thanks David, I'll hit on this again on another day.


Cheers, Gene Heskett
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page <http://geneslinuxbox.net:6309/gene>

[toc] | [prev] | [next] | [standalone]


#168781

FromDavid Wright <deblis@lionunicorn.co.uk>
Date2016-06-10 07:40 +0200
Message-ID<rInkK-vX-37@gated-at.bofh.it>
In reply to#168780
On Fri 10 Jun 2016 at 01:04:40 (-0400), Gene Heskett wrote:
> On Thursday 09 June 2016 23:50:35 David Wright wrote:
> > On Thu 09 Jun 2016 at 22:41:27 (-0400), Gene Heskett wrote:
> > > A bash script that has worked most of a decade now refuses.
> > >
> > > For instance, assume that var InMail is = "gene", and can be echoed
> > > from the command line using $InMail like this.
> > > gene@coyote:~$ echo $InMail
> > > gene
> > > But I'll be switched if I can get a result from a line of code
> > > resembling this from the command line while attempting to
> > > troubleshoot a 110 line bash script: which asks "if test [${InMail}
> > > = "gene"]
> > > 			  then
> > > 				-----
> > > 			 elif (another name)
> > > 				yadda yadda
> > >
> > > gene@coyote:~$ echo `test [${InMail} = "gene"]`
> > >
> > > All I get is the linefeed.  Obviously I'm losing it, so how do I
> > > translate and get usefull output for troubleshooting?
> >
> > $ `[ ${InMail} = "gene" ]` ; echo $?
> > 0
> > $ `[ ${InMail} = "xgene" ]` ; echo $?
> > 1
> > $ `test ${InMail} = "gene"` ; echo $?
> > 0
> > $ `test ${InMail} = "xgene"` ; echo $?
> > 1
> > $
> >
> > elif needs [], not (), and it needs a then too.
> >
> > [ is a command like test, so it must be followed by a space
> > before its argument.
> >
> > Often the easiest way of solving these is grep -A ... used with
> > /etc/init.d/*
> 
> This script is home brew, and has never had a starter in /etc/init.d

Um, not my intention to make you try.

Look, /etc/init.d/* is a collection of scripts that work.
grep searches files for patterns.
Put them together and you can bash away by example. Try

$ grep -A 6 -B 6 elif /etc/init.d/* | less

and you will get lots of examples of multiple tests involving strings
(like yours), numbers, arithmetic xpressions etc. all syntactically
correct. Copy what you need, either from there or the original file.

Cheers,
David.

[toc] | [prev] | [next] | [standalone]


#168784

FromGene Heskett <gheskett@shentel.net>
Date2016-06-10 08:40 +0200
Message-ID<rIogN-16D-15@gated-at.bofh.it>
In reply to#168781
On Friday 10 June 2016 01:34:14 David Wright wrote:

> On Fri 10 Jun 2016 at 01:04:40 (-0400), Gene Heskett wrote:
> > On Thursday 09 June 2016 23:50:35 David Wright wrote:
> > > On Thu 09 Jun 2016 at 22:41:27 (-0400), Gene Heskett wrote:
> > > > A bash script that has worked most of a decade now refuses.
> > > >
> > > > For instance, assume that var InMail is = "gene", and can be
> > > > echoed from the command line using $InMail like this.
> > > > gene@coyote:~$ echo $InMail
> > > > gene
> > > > But I'll be switched if I can get a result from a line of code
> > > > resembling this from the command line while attempting to
> > > > troubleshoot a 110 line bash script: which asks "if test
> > > > [${InMail} = "gene"]
> > > > 			  then
> > > > 				-----
> > > > 			 elif (another name)
> > > > 				yadda yadda
> > > >
> > > > gene@coyote:~$ echo `test [${InMail} = "gene"]`
> > > >
> > > > All I get is the linefeed.  Obviously I'm losing it, so how do I
> > > > translate and get usefull output for troubleshooting?
> > >
> > > $ `[ ${InMail} = "gene" ]` ; echo $?
> > > 0
> > > $ `[ ${InMail} = "xgene" ]` ; echo $?
> > > 1
> > > $ `test ${InMail} = "gene"` ; echo $?
> > > 0
> > > $ `test ${InMail} = "xgene"` ; echo $?
> > > 1
> > > $
> > >
> > > elif needs [], not (), and it needs a then too.
> > >
> > > [ is a command like test, so it must be followed by a space
> > > before its argument.
> > >
> > > Often the easiest way of solving these is grep -A ... used with
> > > /etc/init.d/*
> >
> > This script is home brew, and has never had a starter in /etc/init.d
>
> Um, not my intention to make you try.
>
> Look, /etc/init.d/* is a collection of scripts that work.
> grep searches files for patterns.
> Put them together and you can bash away by example. Try
>
> $ grep -A 6 -B 6 elif /etc/init.d/* | less
>
> and you will get lots of examples of multiple tests involving strings
> (like yours), numbers, arithmetic xpressions etc. all syntactically
> correct. Copy what you need, either from there or the original file.
>
> Cheers,
> David.

Ah, I see.  Been guilty of that before, but my memory isn't what it was 
even 40 years ago, darn it.

Cheers, Gene Heskett
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page <http://geneslinuxbox.net:6309/gene>

[toc] | [prev] | [next] | [standalone]


#168776

FromDavid Christensen <dpchrist@holgerdanske.com>
Date2016-06-10 06:10 +0200
Message-ID<rIlVD-887-1@gated-at.bofh.it>
In reply to#168773
On 06/09/2016 07:41 PM, Gene Heskett wrote:
> A bash script that has worked most of a decade now refuses.
>
> For instance, assume that var InMail is = "gene", and can be echoed from
> the command line using $InMail like this.
> gene@coyote:~$ echo $InMail
> gene
> But I'll be switched if I can get a result from a line of code resembling
> this from the command line while attempting to troubleshoot a 110 line
> bash script: which asks "if test [${InMail} = "gene"]
> 			  then
> 				-----
> 			 elif (another name)
> 				yadda yadda
>
> gene@coyote:~$ echo `test [${InMail} = "gene"]`
>
> All I get is the linefeed.  Obviously I'm losing it, so how do I
> translate and get usefull output for troubleshooting?


'test' and '[ ... ]' are very similar.  I use the latter.  I definitely 
don't use both in one line.


When comparing variables against string constants in '[ ... ]' 
expressions, I put quotes around the variables.


Here's a short script that seems to work correctly:

	2016-06-09 20:48:40 dpchrist@t7400 ~/sandbox/bash
	$ cat gene-heskett.sh
	#!/bin/bash
	export InMail="gene"
	echo "InMail=${InMail}"
	if [ "${InMail}" = "gene" ]
	then
	    echo "InMail is gene"
	elif [ "$InMail" = "david" ]
	then
	    echo "InMail is david"
	else
	    echo "InMail unknown"
	fi


Here's a run:

	2016-06-09 20:49:30 dpchrist@t7400 ~/sandbox/bash
	$ ./gene-heskett.sh
	InMail=gene
	InMail is gene


Here's a run with the '-x' option:

	2016-06-09 20:56:19 dpchrist@t7400 ~/sandbox/bash
	$ bash -x gene-heskett.sh
	+ export InMail=gene
	+ InMail=gene
	+ echo InMail=gene
	InMail=gene
	+ '[' gene = gene ']'
	+ echo 'InMail is gene'
	InMail is gene


If that doesn't help, post a complete script that demonstrates the problem.


David

[toc] | [prev] | [next] | [standalone]


#168779

FromGene Heskett <gheskett@shentel.net>
Date2016-06-10 07:10 +0200
Message-ID<rImRH-lD-1@gated-at.bofh.it>
In reply to#168776
On Friday 10 June 2016 00:00:12 David Christensen wrote:

> On 06/09/2016 07:41 PM, Gene Heskett wrote:
> > A bash script that has worked most of a decade now refuses.
> >
> > For instance, assume that var InMail is = "gene", and can be echoed
> > from the command line using $InMail like this.
> > gene@coyote:~$ echo $InMail
> > gene
> > But I'll be switched if I can get a result from a line of code
> > resembling this from the command line while attempting to
> > troubleshoot a 110 line bash script: which asks "if test [${InMail}
> > = "gene"]
> > 			  then
> > 				-----
> > 			 elif (another name)
> > 				yadda yadda
> >
> > gene@coyote:~$ echo `test [${InMail} = "gene"]`
> >
> > All I get is the linefeed.  Obviously I'm losing it, so how do I
> > translate and get usefull output for troubleshooting?
>
> 'test' and '[ ... ]' are very similar.  I use the latter.  I
> definitely don't use both in one line.
>
>
> When comparing variables against string constants in '[ ... ]'
> expressions, I put quotes around the variables.
>
>
> Here's a short script that seems to work correctly:
>
> 	2016-06-09 20:48:40 dpchrist@t7400 ~/sandbox/bash
> 	$ cat gene-heskett.sh
> 	#!/bin/bash
> 	export InMail="gene"
> 	echo "InMail=${InMail}"
> 	if [ "${InMail}" = "gene" ]
> 	then
> 	    echo "InMail is gene"
> 	elif [ "$InMail" = "david" ]
> 	then
> 	    echo "InMail is david"
> 	else
> 	    echo "InMail unknown"
> 	fi
>
>
> Here's a run:
>
> 	2016-06-09 20:49:30 dpchrist@t7400 ~/sandbox/bash
> 	$ ./gene-heskett.sh
> 	InMail=gene
> 	InMail is gene
>
>
> Here's a run with the '-x' option:
>
> 	2016-06-09 20:56:19 dpchrist@t7400 ~/sandbox/bash
> 	$ bash -x gene-heskett.sh
> 	+ export InMail=gene
> 	+ InMail=gene
> 	+ echo InMail=gene
> 	InMail=gene
> 	+ '[' gene = gene ']'
> 	+ echo 'InMail is gene'
> 	InMail is gene
>
>
> If that doesn't help, post a complete script that demonstrates the
> problem.
>
>
> David

I might just take you up on that when I am ready to add another layer of 
bulletproofing to it.  Its about 110 lines now.
Cheers, Gene Heskett
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page <http://geneslinuxbox.net:6309/gene>

[toc] | [prev] | [next] | [standalone]


#168813

FromDavid Christensen <dpchrist@holgerdanske.com>
Date2016-06-11 05:50 +0200
Message-ID<rII5P-6gx-5@gated-at.bofh.it>
In reply to#168779
On 06/09/2016 10:04 PM, Gene Heskett wrote:
 > And I just amrecover'd a week old version that is working fairly well


On 06/09/2016 10:06 PM, Gene Heskett wrote:
> On Friday 10 June 2016 00:00:12 David Christensen wrote:
>>
>> If that doesn't help, post a complete script that demonstrates the
>> problem.
>
> I might just take you up on that when I am ready to add another layer of
> bulletproofing to it.  Its about 110 lines now.

Another layer and then debug/ test?  Yikes!


As one of the smartest people I knew put it: "Design from the top down, 
but build from the bottom up".


My software works best if I:

1.  Use a source code version control system and do frequent check-ins.

2.  Divide the functionality into little pieces and test each piece 
individually, then test the first level up (assemblies of pieces), then 
the next layer up (assemblies of assemblies of pieces), and so on up to 
the top-level (user interface).

3. Automate as much as possible -- building, testing, installing, 
packaging, releasing, etc..


David


p.s.  A digression -- while Bourne/ Bash shell have some very intriguing 
capabilities and both exist almost universally in FOSS realms, nothing 
has held my fascination since I discovered Perl.  Perl does #2 very 
nicely and includes tools and frameworks for #3.  (I use CVS for #1.)

[toc] | [prev] | [next] | [standalone]


#168787

From<tomas@tuxteam.de>
Date2016-06-10 09:10 +0200
Message-ID<rIoJP-1vU-19@gated-at.bofh.it>
In reply to#168773
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thu, Jun 09, 2016 at 10:41:27PM -0400, Gene Heskett wrote:
> Greetings; 
> 
> A bash script that has worked most of a decade now refuses.
> 
> For instance, assume that var InMail is = "gene", and can be echoed from 
> the command line using $InMail like this.
> gene@coyote:~$ echo $InMail
> gene
> But I'll be switched if I can get a result from a line of code resembling 
> this from the command line while attempting to troubleshoot a 110 line 
> bash script: which asks "if test [${InMail} = "gene"]
> 			  then
> 				-----
> 			 elif (another name)
> 				yadda yadda
> 
> gene@coyote:~$ echo `test [${InMail} = "gene"]`

Most has been answered in this thread here and there, but I think some
things bear repeating:

test and []

  Basically, 'test' and '[' are the same -- well, more or less. There's
  even an executable /usr/bin/[ (no kidding) which in former times was
  just a hardlink to /usr/bin/test (I lost track of why it ain't these
  days). Those days test and/or [ are mostly shell builtins.
  
  So in
  
    if test [ ... ] ; then
  
  either the test or the [] is redundant. Better spell as
  
    if test ...
  
  or
  
    if [ ... ]
  
  depending on your taste.

exit value vs stdout vs command line args

  echo just echoes its command line args. So

    echo foo

  will output foo to stdout

    echo $InMail

  will output 'gene", because the shell will get its first pass through
  the line, will replace $InMail with gene and echo will see 'gene' as
  its first command line arg and do its thing. In

    echo `test [${InMail} = "gene"]`

  the shell again will have a first go. The `` will tell it to interpret
  the thing inside (i.e. test ...) and literally replace its *standard
  output* in place. Test doesn't output anything, thus the whole
  construct `...` gets replaced by nothing, echo sees an empty arg
  and outputs nothing (followed by a newline, which echo does by
  default).

  This is the empty line you are seeing.

  But, you will ask, where's the result of the test gone? Test is
  called for its *exit value*, a number (which by convention is
  0 when everything went OK and different from 0 for some error
  condition). You can use this exit value in the "if" construct
  (also in "while" and so on), and you can extract it from the
  special variabl $?. Also, you can use it in chained logical
  constructs as in "foo && bar || baz" (e.g.:

    rm -f bumble || echo "can't remove this thing"

  would be a more concise way to spell

    if ! rm -f bumble ; then echo "darn" ; fi

Happy hacking!

regards
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAldaZ0cACgkQBcgs9XrR2kacVwCdEMB/VjiRMEHsOHR/f7YW3JCL
yUoAn2p2eeqDDcTZ1g3h6yGFXUY2TWpM
=axhF
-----END PGP SIGNATURE-----

[toc] | [prev] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web