Using scripts to create output files

been messing around with linux for a few months...not too good yet. thinking about taking a class or something, this #### is hard.......anyway, im trying to make an output file using the input from a prompt. heres basically what i have now (random example)

echo -n "Please enter your name: "
read name
##
echo "This is a new file."
echo "It was created by $name."

basically, all ive done is set the variable, im trying to figure out how to take that variable and use it in an output file...i am at a loss, been working for like 2 hours, i have tried a few different things, but not the results im looking for.

would really appreciate any tips

Read up on redirection

echo "hello"

sends to screen

echo "hello" >> myfile

sends that to the file called myfile, in append mode
but beware of > as it creates a new file
as in

echo "hello" >> myfile
echo "goodbye" > myfile
cat myfile

will leave you only with "goodbye"

thanks, I know a little about redirection from the command line, is it the same when I do it from a script? I can't get it to work

Yes, shell script and command lines are usually the exact same language. (Usually, but not always.)

What exactly did you try, and in what way did it "not work"? Be specific.

i am trying to create an output file from a script, here is exactly what i did:

#!/bin/bash
##
##
echo -n "Please enter your name: "
read name
##
echo "This is a new file."
echo "It was created by $name"
 
cat > newfile

i dont know how to make the output end up in the file, the output is going to the standard out (the screen) instead of in the file. newfile is empty.

#!/bin/bash
##
##
echo -n "Please enter your name: "
read name
##
echo "This is a new file." > newfile
echo "It was created by $name" >> newfile
1 Like

Thanks, that works great

read name

when reading from both the file and tty (case basis) you can try this

read name < /dev/tty