Echo output from command | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Echo output from command

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-28-2012
mogabr mogabr is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 10 January 2013, 8:02 AM EST
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Echo output from command

Hello

i am trying to do some calculation from output command

for example

Code:
ls -l
if [ "$ wc -l" -gt 2 ]
then
echo "error"
else
echo "$"
fi

its something like this
which get strings from output command and try to sum total lines of this string if exceed certain number it display error
if not it display output from the command
please note ls -l is just a example
Sponsored Links
    #2  
Old 12-28-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 October 2014, 8:41 PM EDT
Location: San Jose, CA, USA
Posts: 4,882
Thanks: 182
Thanked 1,641 Times in 1,392 Posts
If I understand what you're trying to do, the following should be the syntax you need:

Code:
if [ "$(ls -l|wc -l)" -gt 2 ]
then
        echo 'error'
else
        echo '$'
fi

This won't display the output from the ls command. If you want to display the output from the ls command, change the
Code:
"$(ls -l|wc -l)"

to:
Code:
"$(ls -l|tee /dev/tty|wc -l)"

I don't understand why you want the echo "$" ; it is the default normal user shell prompt and may confuse users if it looks like your script produces two shell prompts.
Sponsored Links
    #3  
Old 12-28-2012
mogabr mogabr is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 10 January 2013, 8:02 AM EST
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Don Cragun View Post
If I understand what you're trying to do, the following should be the syntax you need:

Code:
if [ "$(ls -l|wc -l)" -gt 2 ]
then
        echo 'error'
else
        echo '$'
fi

This won't display the output from the ls command. If you want to display the output from the ls command, change the
Code:
"$(ls -l|wc -l)"

to:
Code:
"$(ls -l|tee /dev/tty|wc -l)"

I don't understand why you want the echo "$" ; it is the default normal user shell prompt and may confuse users if it looks like your script produces two shell prompts.
hello

i give
Code:
ls -l

as example
but i want general operator that display output from last run command
    #4  
Old 12-28-2012
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 21 October 2014, 8:41 PM EDT
Location: San Jose, CA, USA
Posts: 4,882
Thanks: 182
Thanked 1,641 Times in 1,392 Posts
Quote:
Originally Posted by mogabr View Post
hello

i give
Code:
ls -l

as example
but i want general operator that display output from last run command
So use one of the examples I gave you, but replace ls -l and wc -l with the commands you want to use.

If you would tell us what you really want to do instead of a fake example that reports an error if there is more than one file (with a name not starting with a period (".")) in a directory, we might be able to give you a template that will actually do what you want done.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need help in echo output ranjancom2000 Shell Programming and Scripting 3 07-22-2011 05:10 AM
echo two command output in the same line chuikingman Shell Programming and Scripting 4 01-25-2011 01:38 AM
problem with suppressed output to file using echo and tee command shahanali Shell Programming and Scripting 1 09-20-2010 07:09 PM
Format output from "echo" command msb65 UNIX for Dummies Questions & Answers 2 08-05-2010 09:24 PM
Echo and a command's output on the same line codyhazelwood Shell Programming and Scripting 7 03-23-2010 07:24 PM



All times are GMT -4. The time now is 09:37 PM.