display echo only once


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting display echo only once
# 1  
Old 04-25-2012
display echo only once

lets say I am printing something out

Code:
echo "Please enter a valid username"

and its being printed out 5 times, is there any way I can limit to only being displayed ONCE. I tried echo -n but that just makes everything fit on one line.

Right now it keeps saying
Code:
Please enter a valid username
Please enter a valid username
Please enter a valid username
Please enter a valid username
Please enter a valid username
Please enter a valid username
Please enter a valid username

and I want it displayed only once

thanks
# 2  
Old 04-25-2012
Do you have it in a loop? Sounds to me like something other than the echo is your problem.

It would be helpful to see your script along with the shell that you are using and O/S.
# 3  
Old 04-25-2012
Quote:
Originally Posted by agama
Do you have it in a loop? Sounds to me like something other than the echo is your problem.

It would be helpful to see your script along with the shell that you are using and O/S.
sure thing it is a for loop

Code:
#!/bin/bash

for img in *.jpg
do
        if [ $# -gt 0 ]
then
        jpegtopnm "$img" | pnmscale -height 200 | pnmtojpeg > "${img%.jpg}-thumb.jpg"
        echo $img
else
        echo -n  "Please provide a proper image file"
fi
shift
done

what its doing is taking any file ending in .jpg in the current directory and making it into a thumbnail. I am running into a problem where I want it to display the error message only if no arguments are provided on the command line. I am not doing it right Smilie

Its my fault for trying to make it convert everything in the directory ending in .jpg using the FOR loop but I'm at the same time trying to feed an argument on the command line . . I am in a pickle... I would much rather just feed it command line but at the same time have it being able to take 1 argument or even a list but still give an error if no arguments are being displayed. Im new to this sorry. Any help is appreciated.
# 4  
Old 04-25-2012
bad post

Last edited by 47shailesh; 04-25-2012 at 09:43 PM..
# 5  
Old 04-25-2012
First off, there seems to be some confusion in your code:

Code:
for img in *.jpg
do
        if [ $# -gt 0 ]
            ....

        shift
done

What is the for loop supposed to cycle through? The arguments given or the files with a certain ending?

Apart from that, one of the three utilities in this pipeline:

Code:
jpegtopnm "$img" | pnmscale -height 200 | pnmtojpeg > "${img%.jpg}-thumb.jpg"

now print an error message every time some error condition occurs. As the condition occurs repeatedly the message is printed several times.

If you want to print it only once use a flag (which you raise when the condition occurs) and only at the end print the message depending on the status of the flag. The usual way to do this is to redirect error messages to "/dev/null" and use the exit code of the program(s) as indication if everything has gone ok.

So, your code should look something like this:

Code:
#!/bin/bash

local lErrorFlag=0

for img in *.jpg
do
        if [ $# -gt 0 ]
then
        jpegtopnm "$img" | pnmscale -height 200 | pnmtojpeg > "${img%.jpg}-thumb.jpg" 1>/dev/null 2>/dev/null
        if [ $? -gt 0 ] ; then
             lErrorFlag=1
        fi
        echo $img
else
        echo -n  "Please provide a proper image file"
fi
shift
done

if [ $lErrorFlag -gt 0 ] ; then
     echo "<your error message here>"
fi

But there is still some gotcha: in pipelines only one return code is given back and it not completely standardized which one this is. Depending on which OS and shell you use and which options you use (see for instance "set -o pipefail" in bash) you might get differing results.

I hope this helps.

bakunin
# 6  
Old 04-25-2012
You have a couple of options. Test before entering the for loop and exit immediately after writing the error message:

Code:
if (( $# < 1 ))
then
   echo "you must enter a name to convert"
   exit
fi

ls *.jpg | while read img
do
    jpegtopnm "$img" | pnmscale -height 200 | pnmtojpeg > "${1%.jpg}-thumb.jpg"
done



However, since you are not using the information on the command line it seems odd that you would require it be entered. Maybe you are intending to get parameters or something from the command line; if that is the case then this would allow for you to validate and continue if you get the information.

However, the message in your code implies that you want the user to enter an image file to convert. If they enter a file, do you want to just convert the ones on the command line, but default to converting everything if they don't enter any filename? If so, you could do something like this:

Code:
function mk_thumb
{
    # probably wise to add code to verify that $1 is an image file, exists etc.
    jpegtopnm "$img" | pnmscale -height 200 | pnmtojpeg > "${1%.jpg}-thumb.jpg"
}

if (( $# > 0 ))             # if command line parms, process just them
then
    while [[ -n $1 ]]
    do
        mk_thumb $1
        shift
    done
else                        # no parms, then convert all
    ls *.jpg | while read img
    do
        mk_thumb $img
    done
fi



This invokes the conversion function once for each command line parm (the function probably should verify that it exists and is a proper file type etc.). If no parameters are entered, then it invokes the function for all jpg files in the current directory.

I've not really tested either of these, but you should get an idea of your options.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Using ls or echo to display a specific output

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: What single command line would you enter to get the following output? 8140 drwxr-xr-x 9 root bin 18 Jan 20... (6 Replies)
Discussion started by: dasboot
6 Replies

2. Shell Programming and Scripting

Display and write in file in one echo command

Hi All, I want to display content on command promt and also write in file. For that iI ahve to write two sentence echo "XXXXXXX" echo "XXXXXXXX" >> 1.txt Is there any way to write in one echo statement (1 Reply)
Discussion started by: vivek1489
1 Replies

3. Shell Programming and Scripting

echo display problem

Hi I am facing a strange problem a=03 echo ${a} the output is 3 But i want to display it is 03 Can you people help me how to display it like 03. Thanks (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

4. Shell Programming and Scripting

Display echo results in three column

Dear Friends, I have my command output which displays on one row and values are now scrollable (vertical) 3 pages. How do i display those output in three column so that i no need to scroll? Example: dcadd$cat components 1.Caluculator 2.Diary ... ... 50.Mobile 51.Battery .. ...... (12 Replies)
Discussion started by: baluchen
12 Replies

5. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

6. Shell Programming and Scripting

Need a Command To display "echo command value in loop" in single line.

Hi I want to display "echo command value in loop" in single line. My requirement is to show the input file (test_1.txt) like the output file (test_2.txt) given below. Input file :test_1.txt a1|b1|4|5 a1|b1|42|9 a2|b2|32|25 a1|b1|2|5 a3|b3|4|8 a2|b2|14|6 Output file:test_2.txt... (2 Replies)
Discussion started by: sakthifire
2 Replies

7. Shell Programming and Scripting

what does echo $$ command display

whats the value stored in $$ (2 Replies)
Discussion started by: suri
2 Replies

8. Shell Programming and Scripting

Echo display alignment/sort order

Hi, My script prints a few varibales as each it reads each line of a text file and then prints them on screen, however iam having problem in aligning and sorting them. what happens is this Last First Number Mark leo 87798798... (1 Reply)
Discussion started by: shackman66
1 Replies

9. UNIX for Dummies Questions & Answers

Display from a variable using echo.

I have a variable that is outputting a lot of space. here has been 45 lines returned ... how can I remove the spaces between the "been and the 45" CODE: fil_len=`wc -l < coshb.txt` if ; then cat coshb.txt | more echo " " echo "There has been ${fil_len} lines... (4 Replies)
Discussion started by: jagannatha
4 Replies
Login or Register to Ask a Question