2

I created a simple shell script:

#!/bin/bash
clear
echo "Starting Script now....."
echo "Write the info below to a new file in same directory...."

echo "name: John Smith"
echo "email: [email protected]
echo "gender: M"
echo 
echo
echo "File is done"

I want to create a file in the same directory with the Name, email, and gender details. I don't want to do it from the command line like this:

#./script.sh > my.config

I'd rather do it from within the file itself.

4 Answers 4

15

Heredoc.

cat > somefile << EOF
name: ...
 ...
EOF
Sign up to request clarification or add additional context in comments.

Comments

4

You can just do:

#!/bin/bash
clear
echo "Starting Script now....."
echo "Write the info below to a new file in same directory...."

# save stdout to fd 3; redirect fd 1 to my.config
exec 3>&1 >my.config

echo "name: John Smith"
echo "email: [email protected]"
echo "gender: M"
echo 
echo

# restore original stdout to fd 1
exec >&3-

echo "File is done"

Comments

3

Well, just add >> yourfile to the echo lines you want to write :

echo "name: John Smith" >> yourfile
echo "email: [email protected]" >> yourfile
echo "gender: M" >> yourfile

Comments

0

For all your echo "name:John Smith" lines add a > $1 (ie first parameter passed in to the script).

Then run the script like ./script.sh my.config.

Or you could replace the $1 with my.config and just run ./script.sh.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.