Find the answer to your Linux question:
Results 1 to 3 of 3
Hi I'm fairly new to bash scripting so please bear with me if i have missed something obvious I'm trying to make a little script that shows a "up"/"down" status ...
  1. #1
    Just Joined!
    Join Date
    Feb 2012
    Posts
    3

    printf doesn't align the output to the right when in bash script

    Hi

    I'm fairly new to bash scripting so please bear with me if i have missed something obvious

    I'm trying to make a little script that shows a "up"/"down" status for a handful of applications upon ssh login.

    I want the output to show something like this:

    apache up
    proftpd up
    mysql_server up
    /dev/sda down

    explained, i just want the up/down variable to be aligned to the right side of the screen/terminal.

    I tried something like this:

    Code:
    sda1=`if [ $sda == "standby" ]; then
    printf "%${COLUMNS}s\n" "\e[0;33mdown\e[m"
    else printf "%${COLUMNS}s\n" "\e[0;32mup\e[m"
    fi`
    
    echo -e "/dev/sda $sda1"
    but that just outputs:
    Code:
    /dev/sda up
    ...without the text alignment.

    If i just write the printf cmd in a bash window, it aligns the output to the right as it should.

    It seems like it ignores the alignment when it's being run in in a script?

    How can I get the variable-part of each line in the output to be aligned to the right?

  2. #2
    Just Joined!
    Join Date
    Feb 2012
    Posts
    3
    bump

    Any help would be greatly apprecieated!

  3. #3
    Linux User Krendoshazin's Avatar
    Join Date
    Feb 2005
    Location
    London, England
    Posts
    385
    If you echo $COLUMNS from the script you'll see that it returns nothing, but if you echo it from the console it returns the correct value - hence the reason it doesn't work. Try putting COLUMNS=`tput cols` at the start of your script.

Posting Permissions

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