You are not logged in.
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
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
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