Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-14 Thread Glynn Clements

Vaclav Petras wrote:

  I remember that we had this conversation recently. What about putting some
 (ba)sh(ell) primer into the manual?

Users aren't required to use bash. Windows users (if they ever leave
the GUI) will typically use cmd.exe, Unix users might use csh, ksh or
zsh.

IMNSHO, the correct place for information on using bash is the bash(1)
manual page, not in the manual pages of every command which could
conceivably be run from bash.

-- 
Glynn Clements gl...@gclements.plus.com
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev


Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-14 Thread Glynn Clements

Vaclav Petras wrote:

 And about Bash versus Python, I agree, I feel it the same, but at the end,
 I always ends with Python. By the way, there is actually one thing which is
 sometimes used and that is generation of bash code/script in Python and
 then running it using bash command. Bit strange but powerful.

And dangerous (which is why the use of system() and popen() has been
eradicated in 7.0).

And non-portable; for 7.0, Windows users shouldn't need to have bash
(or any Unix tools) installed.

-- 
Glynn Clements gl...@gclements.plus.com
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev


Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-13 Thread Hamish
Nikos wrote:
 for Image in `echo $GIS_OPT_MSX | tr , \ ` ; do echo ${Image}  ;  done

Curly brackets do nothing to protect the contents of a variable. Using them
tricks new bash coders into thinking they are protected when they are not.
To avoid propagating the damaging myth, only use them when you actually need
them (eg to protect the variable name from following characters, not its 
contents).

Vaclav:
 Is GIS_OPT_MSX also in GRASS 7 (trunk)?

That's just going to be the contents of a msx= command line option defined in 
the
script header's #% definition.


regards,
Hamish
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev


Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-13 Thread Nikos Alexandris
Nikos wrote:

  for Image in `echo $GIS_OPT_MSX | tr , \ ` ; do echo ${Image}  ; 
  done

Hamish wrote:

 Curly brackets do nothing to protect the contents of a variable. Using them
 tricks new bash coders into thinking they are protected when they are not.
 To avoid propagating the damaging myth, only use them when you actually need
 them (eg to protect the variable name from following characters, not its
 contents).

Thank you Hamish.

Vaclav:

  Is GIS_OPT_MSX also in GRASS 7 (trunk)?
 
 That's just going to be the contents of a msx= command line option defined
 in the script header's #% definition.

That's right.

Nikos
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-13 Thread Glynn Clements

Hamish wrote:

 Nikos wrote:
  for Image in `echo $GIS_OPT_MSX | tr , \ ` ; do echo ${Image}  ;  done
 
 Curly brackets do nothing to protect the contents of a variable. Using them
 tricks new bash coders into thinking they are protected when they are not.
 To avoid propagating the damaging myth, only use them when you actually need
 them (eg to protect the variable name from following characters, not its 
 contents).

In case it wasn't clear: putting the variable name in braces prevents
subsequent alphanumeric characters from being interpreted as part of
the variable name; e.g.

echo ${hello}_world

will substitute the value of the variable hello, whereas

echo $hello_world

will substitute the value of the variable hello_world.

But if you want to prevent the substituted value from being re-parsed,
double quotes must be used, e.g.:

echo $hello

Example:

$ hello='hi there'
$ echo $hello
hi there
$ echo $hello
hi there

-- 
Glynn Clements gl...@gclements.plus.com
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev


Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-13 Thread Vaclav Petras
On Wed, Nov 13, 2013 at 10:50 AM, Glynn Clements
gl...@gclements.plus.comwrote:


 Hamish wrote:

  Nikos wrote:
   for Image in `echo $GIS_OPT_MSX | tr , \ ` ; do echo ${Image}  ;
 done
 
  Curly brackets do nothing to protect the contents of a variable. Using
 them
  tricks new bash coders into thinking they are protected when they are
 not.
  To avoid propagating the damaging myth, only use them when you actually
 need
  them (eg to protect the variable name from following characters, not its
 contents).

 In case it wasn't clear: putting the variable name in braces prevents
 subsequent alphanumeric characters from being interpreted as part of
 the variable name; e.g.

 echo ${hello}_world

 will substitute the value of the variable hello, whereas

 echo $hello_world

 will substitute the value of the variable hello_world.

 But if you want to prevent the substituted value from being re-parsed,
 double quotes must be used, e.g.:

 echo $hello

 Example:

 $ hello='hi there'
 $ echo $hello
 hi there
 $ echo $hello
 hi there

 I remember that we had this conversation recently. What about putting some
(ba)sh(ell) primer into the manual?

However, I must say the same I said before. If you need some advanced bash
functionality such as `if` and `for` or even functions it is better to
use/learn Python instead.

--
 Glynn Clements gl...@gclements.plus.com
 ___
 grass-dev mailing list
 grass-dev@lists.osgeo.org
 http://lists.osgeo.org/mailman/listinfo/grass-dev

___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-13 Thread Hamish


Glynn:
Example:

        $ hello='hi     there'
        $ echo $hello
        hi there
        $ echo $hello
        hi     there


here's another example:
FOO=a b
BAR=c d
mkdir ${FOO}
mkdir $BAR
ls -l
drwxr-xr-x  a/
drwxr-xr-x  b/
drwxr-xr-x  c d/


Vaclav:
 I remember that we had this conversation recently.

I tend to repost that lesson every time I see curly brackets, in a quixotic 
attempt to stamp them out. :)


 What about putting some (ba)sh(ell) primer into the manual?

My concern with that is where does it end? Do we add a UNIX primer too? I think 
it's a very good idea to put links in the wiki to existing tutorials though, 
but no need for us to rewrite what's already been written many times. Some tips 
are already in the SUBMITTING_SCRIPTS files. Often the best advice is to just 
use the existing GRASS scripts as a template. (be it python or bourne scripting)

 However, I must say the same I said before. If you need some
 advanced bash functionality such as `if` and `for` or even
 functions it is better to use/learn Python instead.

I humbly disagree, but probably only on what would be
considered 'advanced functionality'. I'd put the bar more at
the subtle quoting for dynamic variable names needing `eval`
and nested multi-level $(``) executions, or needing to write
some complicated awk or regex statement. i.e. once it gets to
the point where it isn't human readable anymore.. but I guess
the discussion is highly academic since we're not proposing to
change anything from what we do now.



For manipulating huge amounts of ascii data there's not
much in the world that can beat the UNIX powertools piped
together in a shell script. Also it's hard to beat the
simplicity of cutting and pasting the last five lines
you entered onto the command line into a text editor
and saving the file with a .sh extension.  i.e. a gentle
introduction to programming for command line users. (this
is why I really like having the command line self-assemble at the bottom of the 
module GUIs)

Both python and shell scripting are fine tools, each with their
own niche; I would hope we could identify the best tool for the
job based on the niche, and not devolve into me trying to use
shell scripting for everything because I know it well, and
others trying to use python for everything because they know it
well. And rather both use the most appropriate tool for the job
on a needs basis, and get better in both. :)


2c,
Hamish

___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev


Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-13 Thread Nikos Alexandris
Nikos wrote:

  for Image in `echo $GIS_OPT_MSX | tr , \ ` ; do echo ${Image}  ; 
  done

Hamish wrote:
 
 Curly brackets do nothing to protect the contents of a variable. Using them
 tricks new bash coders into thinking they are protected when they are not.
 To avoid propagating the damaging myth, only use them when you actually need
 them (eg to protect the variable name from following characters, not its
 contents).

Maybe this should be added in /grass_addons/SUBMITTING_SCRIPTS ?

Nikos

___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-13 Thread Vaclav Petras
On Wed, Nov 13, 2013 at 7:47 PM, Nikos Alexandris
n...@nikosalexandris.netwrote:

  Nikos wrote:



   for Image in `echo $GIS_OPT_MSX | tr , \ ` ; do echo ${Image}  ;

   done



 Hamish wrote:

   Curly brackets do nothing to protect the contents of a variable. Using
 them

  tricks new bash coders into thinking they are protected when they are
 not.

  To avoid propagating the damaging myth, only use them when you actually
 need

  them (eg to protect the variable name from following characters, not its

  contents).



 Maybe this should be added in /grass_addons/SUBMITTING_SCRIPTS ?


Hamish and Nikos, SUBMITTING is a great idea. That's the best place. And
once #2103 will be solved it will be ideal. The document can be much longer
then now, I believe.

I'm just not sure if this is for addons or trunk but just but it anywhere
for #2103 they might be merged.

https://trac.osgeo.org/grass/ticket/2103

And about Bash versus Python, I agree, I feel it the same, but at the end,
I always ends with Python. By the way, there is actually one thing which is
sometimes used and that is generation of bash code/script in Python and
then running it using bash command. Bit strange but powerful.

Vaclav



 Nikos



___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-13 Thread Nikos Alexandris
Vaclav Petras wrote:

 However, I must say the same I said before. If you need some 
advanced bash
 functionality such as `if` and `for` or even functions it is better 
to
 use/learn Python instead.

Check the just posted i.fusion.hpf script, you'll love the amount 
of ifs :D.  Just kidding of course. For sure Python can handle such 
situations much more flexible.

Nikos
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-12 Thread Glynn Clements

Nikos Alexandris wrote:

 Is it wrong to simply:
 
 for Image in `echo $GIS_OPT_MSX | tr , \ ` ; do echo ${Image}  ;  done

That will work, provided that none of the values contain spaces.
But setting IFS is more robust and efficient.

-- 
Glynn Clements gl...@gclements.plus.com
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev


Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-12 Thread Nikos Alexandris
Nikos Alexandris wrote:

  Is it wrong to simply:
  for Image in `echo $GIS_OPT_MSX | tr , \ ` ; do echo ${Image}  ;  done

Glynn Clements wrote:

 That will work, provided that none of the values contain spaces.
 But setting IFS is more robust and efficient.

Then I'll keep the IFS trick. Note (to myself): don't forget to *reset* to 
default... 
otherwise:

http://unix.stackexchange.com/questions/98820/why-does-a-working-standalone-nested-function-script-not-work-inside-a-larger-sc

Thanks a million, Nikos

___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

Re: [GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-12 Thread Vaclav Petras
On Tue, Nov 12, 2013 at 10:14 AM, Nikos Alexandris
n...@nikosalexandris.netwrote:

  Nikos Alexandris wrote:



   Is it wrong to simply:

   for Image in `echo $GIS_OPT_MSX | tr , \ ` ; do echo ${Image} ;
 done



 Glynn Clements wrote:



  That will work, provided that none of the values contain spaces.

  But setting IFS is more robust and efficient.



Is GIS_OPT_MSX also in GRASS 7 (trunk)?


  Then I'll keep the IFS trick. Note (to myself): don't forget to *reset*
 to default... otherwise:




 http://unix.stackexchange.com/questions/98820/why-does-a-working-standalone-nested-function-script-not-work-inside-a-larger-sc



 Thanks a million, Nikos



 ___
 grass-dev mailing list
 grass-dev@lists.osgeo.org
 http://lists.osgeo.org/mailman/listinfo/grass-dev

___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

[GRASS-dev] Using a multi-${GIS_OPT_X} input in a shell for loop

2013-11-09 Thread Nikos Alexandris
In past communications in the list I have found Glynn's, if I remember well, 
following piece of 
code-suggestion (or similar):

# set to comma ===
IFS=,

# loop over...
for Image in ${GIS_OPT_MSX}
  do
# check if Image exists
eval `g.findfile element=cell file=${Image}`
if [ -z $name ]
  then g.message -e can't find the ${Image} image.
exit 1
  else g.message -v * Low resolution image:${Image}
fi
done

# reset IFS ==
IFS=${Default_IFS}



Is it wrong to simply:

for Image in `echo $GIS_OPT_MSX | tr , \ ` ; do echo ${Image}  ;  done

Thank you, Nikos
___
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev