You are not logged in.

#1 2012-02-01 14:09:03

haiku
Member
From: Chambery, France
Registered: 2011-01-28
Posts: 19

[SOLVED]bash: assign a command output to a variable

EDIT: Sorry, got the solution - the variable was good, I just forgot a "do" in a "for" loop.... Sorry




Hi erveryone.
I'm just starting to learn the basics of bash scripting, and I got a problem assigning the output of a command to a variable, and i keep getting syntax errors...


var=$(cut -d: -f1 foo)
echo $var

A google search gives many different ways to do this, but without better results..

var=`cut -d: -f1 foo`

var="`cut -d: -f1 foo`"

Any idea what I'm doing wrong?

Thanks!

Last edited by haiku (2012-02-01 14:19:00)


Hasta la Singularidad Siempre

Offline

#2 2012-02-01 14:26:51

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [SOLVED]bash: assign a command output to a variable

haiku wrote:
var=$(cut -d: -f1 foo)

var=`cut -d: -f1 foo`

The first two are both 'right' (in bash) but the first is the preferred syntax (in bash).  I think sh only understands the second.  I've never used quotes in a situation like this, but it seems to work.  That said, maybe the problem is the command itself, because all variations work for me:

$ cat foo
genmon1a:genmon1b
genmon2:genmon3:genmon4
$ cut -d: -f1 foo
genmon1a
genmon2
$ D=$(cut -d: -f1 foo)
$ echo $D
genmon1a genmon2
$ D=`cut -d: -f1 foo`
$ echo $D
genmon1a genmon2
$ D="$(cut -d: -f1 foo)"
$ echo $D
genmon1a genmon2
$ D="`cut -d: -f1 foo`"
$ echo $D
genmon1a genmon2

It might help if you could give us some of the 'bigger picture' too: what you are trying to do overall, the contents of foo, what you are trying to do with the variable, etc.

Last edited by alphaniner (2012-02-01 14:31:01)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#3 2012-02-01 15:14:52

haiku
Member
From: Chambery, France
Registered: 2011-01-28
Posts: 19

Re: [SOLVED]bash: assign a command output to a variable

Thanks! Well, as said in the edit, I saw where I was wrong

var=$(cut -d: -f1 foo)

var=`cut -d: -f1 foo`

both work - it's just my for loop that was wrong




To give you the Big Picture, I want to sort (or, to be precise, to have an organised list of) my photo collection by landscape/portrait.

The basic steps in my mind are:

ask feh to gave me a list of the photos with [path/to/picture] [width] [height]  (that's the foo file in my exemple)
if [width] > [height], print [path] to landscapelist
else, print [path] to portraitlist

and probably to have a sort of ratio minimum to send the "sorta 1:1"  pictures to a different file

Anyway, that should be a good exercice to learn the basics of bash scripting...
... but that have to be a journey I need to travel on my own :-)

Last edited by haiku (2012-02-01 15:17:52)


Hasta la Singularidad Siempre

Offline

Board footer

Powered by FluxBB