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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-19-2008, 01:03 PM
|
#1
|
|
Senior Member
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546
Rep:
|
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
|
|
|
|
05-19-2008, 01:26 PM
|
#2
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
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")
|
|
|
|
05-19-2008, 03:17 PM
|
#3
|
|
Senior Member
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
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... 
|
|
|
|
05-19-2008, 03:33 PM
|
#4
|
|
Senior Member
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546
Original Poster
Rep:
|
I have got also that problem with " and ' ... fighthing ...
Code:
if [ "$i" == "`cat ${LOGFILESSHD} | grep "${i}"`" ] ; then
|
|
|
|
05-19-2008, 03:37 PM
|
#5
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
Quote:
Originally Posted by frenchn00b
"< <()" 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.
|
|
|
|
05-19-2008, 04:44 PM
|
#6
|
|
Senior Member
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546
Original Poster
Rep:
|
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"
|
|
|
|
05-19-2008, 05:16 PM
|
#7
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:03 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|