Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Writing a plain script for printing the outputs of `<command>`.

    I'm dealing with this exercise*
    Write a script that upon invocation shows the time and date, lists all logged-in users, and gives the system uptime. The script then saves this information to a logfile.
    and ok: with the tools that I know I've written the following ugly script:
    echo "current date: $CURRENT_DATE"
    echo "current logged in users: $CURRENT_LOGGED_IN_USERS"
    echo "current system uptime: $CURRENT_SYSTEM_UPTIME"
    #  Maybe I should include here an if-statement
    #+ for checking if the path does not exits
    #+ though I think that's not possible ...
    echo "current date: $CURRENT_DATE" > info.log
    echo "current logged in users: $CURRENT_LOGGED_IN_USERS" >> info.log
    echo "current system uptime: $CURRENT_SYSTEM_UPTIME" >> info.log
    How could I improve that? This is the first bash script that I've ever written.

    Thank you.

    * It's taken from the book Advanced Bash-Scripting Guide by Mendel Cooper.

  2. #2

    in this case variables are useless.
    use command list, and tee:
    { firstCmd; secondCmd;} | tee logfile
    that's it.

  3. #3
    hey, your script is not ugly, maybe just a little over-worked. watael has given you exactly what you need, but here are some points as to your code as you gave it:

    1. when getting command output, use this notation:
    instead of backticks, e.g.:
    b/c the former can be nested, whereas the latter cannot. the former way works perfectly well with a single command though. here's a nested example:

    uid=$(id -ru $(whoami))
    2. when in Bash, you can usually use the environmental variable $PWD, instead of making a system call to pwd.

    3. there is no need to cd to your PWD, as you are already there! if you cd into some other dir, though, you can use double vertical bar as a logical "or" operator and catch it, e.g.:
    cd /no/such/dir || echo "No such dir!"
    4. instead of doing > and >> for every op, enclose everything in a function (which is what watael did). then redirect that output to your file, e.g.:

    myfunction() {
      echo foo
      echo bar
    myfunction > /tmp/log
    also note that when redirecting command output in Bash, you can use the number 1 to signify STANDARD OUTPUT (or STDOUT) and 2 to signify STANDARD ERROR (or STDERR), e.g.:
    ls /foo 1> /tmp/foo.log 2> /tmp/foo.err
    the standard output of the "ls" command will go to the foo.log file, and any errors encountered will go to the foo.err file (like if the dir "/foo" did not exist or you did not have the proper permissions to read it). to redirect both STDOUT and STDERR to the same file:
    ls /foo > /tmp/foo.log 2>&1
    make sure the "2>&1" comes after the "> log" part.

    5. don't put "exit" at the end of your bash scripts, it is unnecessary. and if you source the script (. instead of execute it (./, it will log you out!

  4. $spacer_open

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts