4

I'm creating my first script using "The Advanced Bash Scripting Guide".

One of the exercises asks for a script that, once run, saves the output to a log file.

I've managed to create my first basic script but am having trouble with this last part. The script must contain the code to create the logfile but I can only do it separately in the shell.

The file is called myFirstShellScript.txt. When I run ./myFirstShellScript.txt the script runs. After the script runs if I type ./myFirstShellScript.txt > myFirstShellScriptLog in the shell the new file is created with the output. Now, I tried to add this line within the script but the file that was output was blank.

Here's my first script, please don't laugh.

#! /bin/bash    
    # show todays date and time
    echo "Todays date is $(date +%j)."

    # Show who is currently logged into the system
    echo $(who)

    # show system uptime
    echo $(uptime)

    # log output in separate file using redirect

exit

What do I have to do (In as simple English as possible) to have the script create the output file by itself, rather than having to do separately in the shell once run?

2 Answers 2

5

usually enough enclose the parts to ( ), like:

#!/bin/bash
(
    # show todays date and time
    echo "Todays date is $(date +%j)."

    # Show who is currently logged into the system
    echo $(who)

    # show system uptime
    echo $(uptime)
) > myFirstShellScriptLog
Sign up to request clarification or add additional context in comments.

2 Comments

That worked thank you - of the two answers above is one "better"?
Here in "no better". This opens subshell. In some special cases it is not wanted. But, is is simpler as "exec..." and follow the command substitution syntax and like. For you (as for a starting shell programmer) - IMHO - this approach is OK. ;)
4

You can use the exec builtin command to redirect the script's output into a file from within the script:

#! /bin/bash

# Save output to "log.txt"
exec > log.txt

# show todays date and time
echo "Todays date is $(date +%j)."
...

5 Comments

Thanks for you info. Actually, when I did this the file "log" that was created was blank. I think that the instructions had intended for me to use exec though.
Blank?? How are you running the script?
I noticed that if I put exec as the first item in the script it works. When I first tried it I placed it at the end (It just seemed more logical to do that - once the script is done then take the output, was my thinking). Thanks again
It's the exec command that opens the output file and sets up the output redirection. If you want the entire output in the file you have to have it at the top. The benefit of using exec as opposed to the subshell approach of the other answer is that you can have parts of the output of the script in console or in different files.
Thanks for the info Joni I will remember to use exec as I'm learning bash

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.