LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-19-2008, 01:03 PM   #1
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Rep: Reputation: 57
Stupid question: if [ "$i" == `$(cat ${LOGFILESSHD} | grep "${i}" )` ] ; then


Code:
cat /var/log/auth.log  | grep "blabla  blabla blabla"  | while read i ; do 
echo "**$i**" 
allre=`$(cat ${LOGFILESSHD} | grep "${i}" )`
...
the problem is that allre is always egal to nothing

It would work if I had no spaces in "blabla blabla blabla"
and with writing
allre="$(cat ${LOGFILESSHD} | grep ${i} )"
with ".

I have a problem, very stupid , sorry, with " ...
Could somebdy be kind for giving the solution ? I bump bump bump ... on that bash but cannot find it... no idea really

thank you
Best regards
 
Old 05-19-2008, 01:26 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
allre=`$(cat ${LOGFILESSHD} | grep "${i}" )`
this syntax is wrong. the backticks and the syntax $(...) are (almost) equivalent for command substitution. If you use them together you can get unpredictable results. Indeed, at first the command inside $(...) is evaluated, then the result is evaluated again as a command, because of the embedding backticks. You stated that it works using double quotes: what prevent you from using them?

Note that if you're trying to access the variable outside the while loop, it will result empty. This is because the while loop is called after a pipe, hence in its own subshell with its own local variables. Once the subshell is closed, the variables are lost.

To prevent this behavior, use process substitution, e.g.
Code:
while read i ; do 
echo "**$i**" 
allre=`$(cat ${LOGFILESSHD} | grep "${i}" )`
done < <(cat /var/log/auth.log  | grep "blabla  blabla blabla")
 
Old 05-19-2008, 03:17 PM   #3
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Original Poster
Rep: Reputation: 57
Quote:
Originally Posted by colucix View Post
Code:
allre=`$(cat ${LOGFILESSHD} | grep "${i}" )`
this syntax is wrong. the backticks and the syntax $(...) are (almost) equivalent for command substitution. If you use them together you can get unpredictable results. Indeed, at first the command inside $(...) is evaluated, then the result is evaluated again as a command, because of the embedding backticks. You stated that it works using double quotes: what prevent you from using them?

Note that if you're trying to access the variable outside the while loop, it will result empty. This is because the while loop is called after a pipe, hence in its own subshell with its own local variables. Once the subshell is closed, the variables are lost.

To prevent this behavior, use process substitution, e.g.
Code:
while read i ; do 
echo "**$i**" 
allre=`$(cat ${LOGFILESSHD} | grep "${i}" )`
done < <(cat /var/log/auth.log  | grep "blabla  blabla blabla")
hi, thank you hence this should maybe be withotut the $ to make it as command:
Code:
while read i ; do 
echo "**$i**" 
allre=`cat ${LOGFILESSHD} | grep "${i}" `
done < <(cat /var/log/auth.log  | grep "blabla  blabla blabla")
but what is suprising is that line:
Code:
 done < <(cat /var/log/auth.log  | grep "blabla  blabla blabla")
I didnt know that we could use ( ) for commands to bound them right and left, it's like the : '

"< <()" is wicked command. I knew >> and >


Ah, still in the same subject, I also get this < pipeline problem:
Code:
ls -tr | tail -n 1 | xargs mplayer
cannot work when the filename has some spaces...
 
Old 05-19-2008, 03:33 PM   #4
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Original Poster
Rep: Reputation: 57
I have got also that problem with " and ' ... fighthing ...

Code:
if [ "$i" == "`cat ${LOGFILESSHD} | grep "${i}"`" ] ; then
 
Old 05-19-2008, 03:37 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by frenchn00b View Post
"< <()" is wicked command. I knew >> and >
This syntax is specific to process substitution. For details and some more examples, have a look at chapter 22 of the Advanced Bash Scripting Guide.
Quote:
Ah, still in the same subject, I also get this < pipeline problem:
Code:
ls -tr | tail -n 1 | xargs mplayer
cannot work when the filename has some spaces...
You can try
Code:
ls -tr -Q | tail -n 1 | xargs mplayer
The -Q option embeds file names in double quotes, so that when it is passed as argument to xargs the blank space is correctly interpreted by the shell.
 
Old 05-19-2008, 04:44 PM   #6
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Original Poster
Rep: Reputation: 57
Not easy;

this works :
Code:
essai=$(host  `cat ~/logs.log  | awk ' { print $11 }  '` ) ; echo $essai
but if I put the ", then not working anymore
Code:
essai="$(host  `cat ~/logs.log  | awk ' { print $11 }  '` ) ; echo $essai"
 
Old 05-19-2008, 05:16 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
essai="$(host  `cat ~/logs.log  | awk ' { print $11 }  '` ) ; echo $essai"
Hmmm... can you explain in real words what you are trying to achieve by this command? What is the expected output and what exactly the problem is? Error messages? Unexpected behavior? Can you provide a real example?

At a first glance, it does not make sense. In the first version - without double quotes - it assigns to the variable essai the output of the command enclosed in $(...) then echoes the value of essai. The second version - with double quotes - assigns the output of the command concatenated with the string " ; echo something", where something is the value of essai. I miss what's your aim and what the problem is, sorry.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie question: whats the difference between "su root", "su" and "su -&quo mojarron Slackware 9 12-07-2009 04:08 PM
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 07:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration