Bash scripting not working due to " or ' ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash scripting not working due to " or ' ?
# 1  
Old 05-12-2010
Bash scripting not working due to " or ' ?

My scripts works fine apart when I let users enter a "grep string" so they can specify what to grep for.

Basically if the user wants to enter for example
Code:
grep "facebook.com | grep "login"

the script would use that grep command for the below bash script section and write the output to $filetowriteWW. Only problem is nothing happens... Smilie

Code:
if [ -z "$grepcommand" ];then
		/usr/bin/gzcat $logfile >> ~/$filetowriteWW
		else
		/usr/bin/gzcat $logfile | $grepcommand >> ~/$filetowriteWW
	fi

Code:
CHOOSEGREP()
{
until (test "$CHECK" = "Y" || test "$CHECK" = "y")
        do
        echo -n "Enter any grep queries you want to search for, leave BLANK if you do not want to filter anything"
		echo "If you search for an IP address and it does not have 3 digits in the last octet grep with a space "
		echo "after the last octect"
		echo "For example: grep \"10.144.123.22 \" |grep \"facebook.com\" | grep -v \"none\"  "
		read grepcommand
		echo "You have choose '$grepcommand' (between the single quotes), is this ok? [y/n]"		
        read CHECK
done
CHECK=""

set -x output
Code:
blah blah other stuff
+ read grepcommand
grep \"facebook.com\" 
+ echo 'You have choose '\''grep "facebook.com"'\'' (between the single quotes), is this ok? [y/n]'
You have choose 'grep "facebook.com"' (between the single quotes), is this ok? [y/n]

etc etc.
now we get to the if $grepcommand" contains a string of not which it does and then this happens...
Code:
+ '[' -z 'grep "facebook.com"' ']'
+ /usr/bin/gzcat /EMEA/fwadmcdc05-ezone.uk.db.com/testproxy/access1004122141.merged-00.15.17.86.ad.70.log.gz
+ grep '"facebook.com"'

Where did my write to file go? and should '"facebook.com"' not be in the same line as the gzcat command and there be a pipe? Smilie

I tried grep 'facebook.com and grep "facebook.com" etc. but no joy

Last edited by GermanJulian; 05-12-2010 at 01:02 PM..
# 2  
Old 05-12-2010
Just because the gzcat and the grep do not occur on the same line in the trace output does not mean that they aren't part of the same pipeline.

Your problem is that you are mistakingly thinking that the double quotes around "facebook.com" are being removed before grep is invoked. They are not. When quotes are introduced after parameter expansion (in this case, $grepcommand), they are not in any way special (unless they are an IFS character, which I assume it's safe to assume that in this case they are not). So, you are grepping for the sitename surrounded in double quotes, the command line equivlanet of: grep \"facebook.com\".

You would need to pass the result of the $grepcommand expansion through eval to achieve what you're after.

Be safe.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Red Hat

Can't boot RHEL7.2 due to "Start Job is Running"

Dear community, as already done tons of times, I prepare the RedHat installation on VMWare, then clone it with clonezilla, and finally restore it on final server. Due to different hrdware, I have to boot from rhel-server-7.4-x86_64-boot.iso cd and rewrite the initramfs that contains the hardware... (3 Replies)
Discussion started by: Lord Spectre
3 Replies

3. UNIX for Beginners Questions & Answers

Automate "touch" bash script not working.

#!/bin/bash -i SAVEIFS=$IFS IFS=$"\n\b" picc=$* if ; then echo $TDATE if ; then touch dummy touch -t "tdate" dummy touch -r "dummy" "$picc" else echo -e "No mod date value to apply. If there is one in your shell,\ninvoke \eStarted asking advice on this (on Linuxquestions.org).... (9 Replies)
Discussion started by: iamwrong
9 Replies

4. Ubuntu

What is solution for this error "tar: Exiting with failure status due to previous errors"?

Does anyone know what is solution for this error ?tar: Exiting with failure status due to previous errors from last 3 days I am trying to take backup of home/user directory getting again and again same error please anyone give me solution (8 Replies)
Discussion started by: Akshay Hegde
8 Replies

5. Shell Programming and Scripting

"Syntax Error sometimes due to corruption of variable value "

Hi , I have script as follows , #!/usr/bin/ksh -x if then alias echo="echo -e" fi MAX_ENTRIES=1024 nb_of_entries=`echo "$list_of_entries" | wc -w` # Set number of tables eval nb_of_tables=\`expr `expr $nb_of_entries / $MAX_ENTRIES` + 1 \` # Output the number of tables echo... (6 Replies)
Discussion started by: breezevinay
6 Replies

6. Shell Programming and Scripting

"Autofill" for bash scripting?

Hi there, I'm trying to arrange the output of my script to the one as follows: -------------- -------- However, after some trial and error, I get this: -------------- ----------- Is there anyway to do an "autofill" so that the portion aligns? I'm using a "for-loop" as i'm... (3 Replies)
Discussion started by: solarisnoob
3 Replies

7. Shell Programming and Scripting

Is this "Out of Memory!" error due to sort() of perl how to resolve it?

I am running a program written in perl script and it is stopped with "Out of memory!" error. This is very strange because at the time then the program is aborted, it only used 4GB RAM and there are still 30GB free physical memory left in the system. I check the perl script and found the program... (3 Replies)
Discussion started by: lilili07
3 Replies

8. Forum Support Area for Unregistered Users & Account Problems

Cannot register due to "spam" error message

Hi all, I am trying to register but it seems my IP address is being seen or black listed as a spam address. I get the following message: "Registration denied. Sorry, The UNIX and Linux Forums runs an active policy of not allowing spammers. Please contact us via by posting in this forum if... (0 Replies)
Discussion started by: codenjanod
0 Replies

9. Shell Programming and Scripting

Script not working..."sort" not working properly....

Hello all, I have a file - 12.txt cat 12.txt =============================================== Number of executions = 2 Total execution time (sec.ms) = 0.009883 Number of executions = 8 Total execution time (sec.ms) = 0.001270 Number of... (23 Replies)
Discussion started by: Rahulpict
23 Replies
Login or Register to Ask a Question