PDA

View Full Version : [SOLVED] Help with bash scripting



SavvasKatseas
October 21st, 2011, 05:19 PM
Hi,

this is a newbie question, so a) if this isn't the right place for it to be feel free to move it and b) if it's easily solveable don't hesitate to just reply with a link where I can read up on it.

I'm trying to write a bash script, and I've included a variable there that points to a file, say:


VARIABLEA=/home/user/script.name

Now, I want a variable B to contain an echo command that writes something to the file which variable A points to. What I've tried so far is:


VARIABLEB=echo "whoo hey hoo hola" >> $VARIABLEA

Which doesn't work. Bash informs me that there's no whoo command. Then I've tried command expansion with `


VARIABLEB=`echo "whoo hey hoo hola" >> $variableA`

Which doesn't work either. Nothing is written to the file.

I'm sure it must a simple thing that I'm missing here and I'd like to know what that is. Any advice or reading material would be appreciated. :)

MG&TL
October 21st, 2011, 05:24 PM
I think you have to declare the value of the variables before you redirect the output of them. I just tried this in an interactive shell (i.e. no script), and the thing works if you declare separately, but not if you don't.

E.g:


variableA="Hello"
variableB="mfyile"
echo $variableA >> $variableB

This is also the standard way of doing things in other languages.

nothingspecial
October 21st, 2011, 05:33 PM
Always quote your variables.

Use $(commands) instead of backticks.

Lars Noodn
October 21st, 2011, 07:47 PM
Always quote your variables.

Use $(commands) instead of backticks.

+1 to both

Also, the program tee (http://manpages.ubuntu.com/manpages/oneiric/en/man1/tee.1.html) allows you to sent output to both stdout and to a file. Though that's probably more use in a different situation, MG&TL's sequence is probably better.

AlphaLexman
October 21st, 2011, 09:47 PM
Hi,

this is a newbie question, so a) if this isn't the right place for it to be feel free to move it and b) if it's easily solveable don't hesitate to just reply with a link where I can read up on it.

I'm trying to write a bash script, and I've included a variable there that points to a file, say:


VARIABLEA=/home/user/script.name

Now, I want a variable B to contain an echo command that writes something to the file which variable A points to. What I've tried so far is:


VARIABLEB=echo "whoo hey hoo hola" >> $VARIABLEA

Which doesn't work. Bash informs me that there's no whoo command. Then I've tried command expansion with `


VARIABLEB=`echo "whoo hey hoo hola" >> $variableA`

Which doesn't work either. Nothing is written to the file.

I'm sure it must a simple thing that I'm missing here and I'd like to know what that is. Any advice or reading material would be appreciated. :)


#!/bin/bash

VARIABLEA="/home/user/script.name"

VARIABLEB=$(echo "whoo hey hoo hola")

echo ${VARIABLEB} >> ${VARIABLEA}

The proper corrections are in red.

Also note that in the OP, "$variableA" is NOT equal to "$VARIABLEA" and *nix systems are case sensitive!

EDIT @MG&TL: you should not have whitespace around the equal sign when defining a variable (probably just a typo right?)

MG&TL
October 21st, 2011, 09:54 PM
@MG&TL: you should not have whitespace around the equal sign when defining a variable (probably just a typo right?)

Yeah. Oops, fixing it, its cool. :)

SavvasKatseas
October 22nd, 2011, 09:43 AM
Thanks a lot for all the help!
It's working now.

I'll try reading a bit more about backticks and $(command).
Tee is also interesting, it will allow me to send something both to the terminal and to a file, if I understood correctly. That'll come in handy :)

mrgs
October 22nd, 2011, 09:47 AM
Moved to Programming Talk.