put script into file

hi, ok having here some code like:

#!/bin/bash
echo " hello "          # say hello to the user
echo " $HOME " 
echo " $HOSTNAME "
echo " $(uname) "
echo " today is $(date) "
echo " $(whoami) is currently logged in  "

now my question is how to change the code so that the info is not displayed but sent to a file ?

I know its a basic thing for you but I am just beginning here so looking forward for your response . .. . .
thanks

(yourscript) > (output.file)

Since you know it was basic why did you not post your question in appropriate forum ?

There is a forum called for dummies you know, or is that offencing?

if moderator thinks it should be moved to dumies forum he/she probably will move it....no not offended

but since we r here, can u change that code to not be displayed just put to file ?

Moved...
Forum rules
(9) Edit your posts if you see spelling or grammar errors (don't write in cyberchat or cyberpunk style). English only.

Have you tested ?

Not wise to ask before having tested given solution...

quick responses and actions :smiley: vbe,

changed and it worked, thanks for that, but got follow up question...

code now looks like that

and my question is can these code be changed so I don't have to type " >> " in every line?

Look:

vi 006
i
#!/bin/bash
echo " hello "          # say hello to the user
echo " $HOME " 
echo " $HOSTNAME "
echo " $(uname) "
echo " today is $(date) "
echo " $(whoami) is currently logged in  "  
:wq
"006" [New file] 7 lines, 184 characters 
an12:/home/vbe $ chmod 750 006
an12:/home/vbe $ ./006 > 006.out
an12:/home/vbe $ more 006.out
 hello 
 /home/vbe 
 an12 
 AIX 
 today is Fri Nov 18 16:43:17 NFT 2011 
 vbe is currently logged in  
006.out: END
 vi 007
#!/bin/bash
FILE=$1

if [ -f $FILE ];then

  echo " hello "  # say hello to the user
  echo " $HOME "
  echo " $HOSTNAME "
  echo " $(uname) "
  echo " today is $(date) "
  echo " $(whoami) is currently logged in "

else
  touch file.007

fi

~
~
~
~
~
"007" [New file] 16 lines, 247 characters 
an12:/home/vbe $ chmod 750 007
an12:/home/vbe $ ./007 >file.007
an12:/home/vbe $ ./007 file.007 >file.007
an12:/home/vbe $ rm file.007
an12:/home/vbe $ ./007 file.007 >file.007
an12:/home/vbe $ more file.007
 hello 
 /home/vbe 
 an12 
 AIX 
 today is Fri Nov 18 17:08:06 NFT 2011 
 vbe is currently logged in 
file.007: END

For you now, I tested both cases with same results, Why?

an12:/home/vbe $ ./007 >file.007
an12:/home/vbe $ ./007 file.007 >file.007

thanks.

would you recommend a script book for me so I can post less dummy questions :slight_smile:

In your code, if FILE does not exist, the script creates it (an empty FILE) and exit. You'll have to run your script twice to have what you expect. I don't know if it's a wanted behaviour.
If you redirect (>>) every time your script runs, the content of all your commands will be appended at the end of the FILE. If you want to start with a fresh FILE every time your script runs, you have to redirect (>) the first echo so FILE won't grow up in size.

Sure, group your commands between { (don't forget the space or new line just after) and } (don't forget space, semi-colon (:wink: or new line just before) and redirect (>) to FILE.

#!/bin/sh
{
echo " hello "
echo " $HOME "
echo " $HOSTNAME "
echo " $(uname) "
echo " today is $(date) "
echo " $(whoami) is currently logged in "
} > "$1"

exit 0

great, thanks
that is what I was thinking about the " { . .. . . . . } exit 0 "

Bash Guide for Beginners
BASH Programming - Introduction HOW-TO
Advanced Bash-Scripting Guide

thanks kalak, will have a look on them at least on the if-then, loops, parts for a start....