grep not accepting variable filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep not accepting variable filename
# 1  
Old 02-20-2011
grep not accepting variable filename

I have a very basic shell script right now, and I am trying to run grep from inside of it. It looks like this:

Code:
#!/bin/sh
filename=$1
echo "The program that you will be editing is called $filename"
grep "//" $filename

The name of said file is "myscript" for right now. I am executing it on the command line as follows:

Code:
sh myscript test.txt

It SHOULD output the first three lines of the text file, since they are preceded with // style comments, which is what I am looking for. It should look like the following:

Code:
// Some text
// Some more text
// Some more text

I know it should look like that because when I run
Code:
grep "//" test.txt

outside of the script, it displays. From inside the script, I get this error:

Code:
The program that you will be editing is called test.txt
grep: can't open test.txt

I'm not sure what I'm doing wrong... I also want to mention that when I put "test.txt" inside the script instead of "$filename", it runs perfectly, so I KNOW it has to be with the variable, but what is it that I messed up?

For added information, the primary shell is tcsh and the shell I am trying to run the script in is sh, if that makes a difference.


Thanks in advance.
# 2  
Old 02-20-2011

What does this print?
Code:
#!/bin/sh
filename=$1
echo "The program that you will be editing is called $filename"
if [ -f "$filename" ]
then
  if [ -r "$filename" ]
  then
     grep "//" "$filename"
  else
     printf "%s is not readable\n" "$filename"
  fi
else
   printf "%s does not exist\n" "$filename"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to grep for a string on a FILENAME?

I call my bash shell script "test.sh" and pass "admin_usr.txt" as an argument like below. ./test.sh admin_usr.txt Inside the "test.sh" i wish to check if the filename passed "admin_usr.txt" i.e "$1" contains the string "admin" or not ... which in this case it does. Note: I do not wish to... (5 Replies)
Discussion started by: mohtashims
5 Replies

2. UNIX for Dummies Questions & Answers

Usage of grep '^$1' filename

There is a file name list_filenames.dat, this has all the list of all files I need to encrypt, I did not understand what the following syntax is doing: grep -s "^$1" list_filenames.dat, when I manually run this command it just returns all the lines, what is the usage of this ? can someone... (4 Replies)
Discussion started by: okkadu
4 Replies

3. Shell Programming and Scripting

sed command not accepting variable in shell script

I am using a shell script in fedora linux. While calling to the shell I am also passing an argument (var1=0.77) like shown below sh gossip.sh var1=0.77 in the shell following command is written (which doesn't work) sed - i -e 's@prob=@prob="$var1";//@g' file.txt Actually i want the... (7 Replies)
Discussion started by: Fakhar Hassan
7 Replies

4. Shell Programming and Scripting

Regex for filename in grep

I want to print the filename keyword="XXTNL_AVSKRIV2ING" ftype="sql' I wan to search the keyword in all the sql files and the output shoul dbe filename:count grep -iwc "$keyword" *.$ftype | grep -v ":0$" But the output does not dispaly the filename which contains space as... (4 Replies)
Discussion started by: millan
4 Replies

5. Shell Programming and Scripting

Diff between grep .* file name and grep '.*' filename

Hi, Can anyone let me know what is difference between grep .* foo.c grep '.*' foo.c I am not able to understand what is exact difference. Thanks in advance (2 Replies)
Discussion started by: SasDutta
2 Replies

6. Shell Programming and Scripting

Accepting multiple values in a variable at run time

Hi, Below is starting entry of my script #!/bin/ksh Usage() { print "Usage: $0 ID OPTION SERVER" print "<br>Where :" print "<br>Enter your ID into PARAM1, OPTION in the PARAM2 and SERVER in the PARAM3 field" print "<br>ID should be a valid ID" print "<br>OPTION should be either... (2 Replies)
Discussion started by: gopajitmalakar
2 Replies

7. Shell Programming and Scripting

variable used as filename

Hello, i'm fairly new to scripting, so please bear with me (I did try looking this up first, i figured it had to have been asked already). #!/bin/bash fileName=`date | sed -n 's/ /_/g p' | sed -n 's/^/Backup_/p' | sed -n 's/$/\.tar/p'`; #THIS SETS BACKUP_DATE echo $fileName #TEST OF VALUE ... (4 Replies)
Discussion started by: jzacsh
4 Replies

8. Shell Programming and Scripting

mv Filename variable to another filename

Anyone who can assist : I am trying to pass the group vairiable to a filename: rpt_tsavegrp=/export/legato/scripts/$group_savegrp_rpt.$dat It will not pass to variable. Anyone have any ideas what I am doing wrong here. Thanks # This script sends email that save group completed.... (3 Replies)
Discussion started by: gzs553
3 Replies

9. Shell Programming and Scripting

Accepting filename as command line param and writing to it

Hi, Is it possible to accept a filename as command line parameter and then write to that file using command redirection? i tried the below script. outputfile=`echo $1` echo "Writing to file" > 'echo $outputfile' exit $returncode but it isnt working. is there any other way to... (9 Replies)
Discussion started by: silas.john
9 Replies

10. Shell Programming and Scripting

using grep and print filename

Hi, I have a question on bash. Basically I would like to print a file name using bash. I am actually trying to grep a particular character in sequential files. I have alot files such that a.txt, b.txt,c.txt...etc. If I found a certain character, I would print that particular filename. I... (5 Replies)
Discussion started by: ahjiefreak
5 Replies
Login or Register to Ask a Question