2

I have created one script with shell scripting and the output of the script is :

enter image description here

but i want to be the output is like this :

enter image description here

and the code is :

if [ $CS == 0 ]; then
printf $BLUE
echo "$url$i            [Found]"
else
printf $RED
echo "$url$i            [Not Found]"
fi
1
  • Your question is unclear. Are you looking for the output to be aligned?
    – fpmurphy
    Commented Oct 17, 2015 at 2:11

1 Answer 1

2

You're already using printf in the true branch of your if statement, which supports formatted output. How about something like this, assuming $CS contains the truth value of your "Found" vs "Not Found" condition:

printf "$color%-50s%s$RESET\n" "$url" "$status"

Where $color is the ANSI code for the desired color, $RESET is the ANSI code \e[0m and $url and $status are the URL string and the [Found] or [Not Found] status strings, respectively.

Here's a full example. Note I've used sh in the shebang, but this is fully compatible with bash syntax as well:

#!/bin/sh

BLUE="^[[0;34m"
RED="^[[0;31m"
RESET="^[[0m"

CS=0

for url in http://example.com/foo http://example.com/longer_address ; do
    if [ $CS -eq 0 ]; then
        color=$BLUE
        status='[Found]'
    else
        color=$RED 
        status='[Not Found]'
    fi

    printf "$color%-50s%s$RESET\n" "$url" "$status"

    CS=1 # Change truth condition for next test
done
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.