Find the answer to your Linux question:
Results 1 to 3 of 3
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2013
    Posts
    2

    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:
    Code:
    #!/bin/bash
    
    CURRENT_DATE=`date`
    CURRENT_LOGGED_IN_USERS=`who`
    CURRENT_SYSTEM_UPTIME=`uptime`
    
    CURRENT_PATH=`pwd`
    
    echo "current date: $CURRENT_DATE"
    echo "current logged in users: $CURRENT_LOGGED_IN_USERS"
    echo "current system uptime: $CURRENT_SYSTEM_UPTIME"
    
    cd $CURRENT_PATH
    #  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
    
    exit
    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
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    226
    hi,

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

  3. #3
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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:
    Code:
    $(command)
    instead of backticks, e.g.:
    Code:
    `command`
    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:

    Code:
    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.:
    Code:
    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.:

    Code:
    myfunction() {
      date
      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.:
    Code:
    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:
    Code:
    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 (. script.sh) instead of execute it (./script.sh), it will log you out!

  4. $spacer_open
    $spacer_close

Posting Permissions

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