LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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, 02:03 PM   #1
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Rep: Reputation: 51
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, 02:26 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 04:17 PM   #3
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 51
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, 04:33 PM   #4
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

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

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

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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, 05:44 PM   #6
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Original Poster
Rep: Reputation: 51
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, 06:16 PM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:08 PM
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 08:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 09:31 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 04:11 PM


All times are GMT -5. The time now is 08:00 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration