Having a little trouble with grep

I am trying to make a script that has grep finding lines that I get from the cat command, that start with something and end with an argument. I can get the first part, but whenever I try to add on the part that looks at the end of the line too, it stops working. Any ideas why? Here is my script:

#!/bin/csh
ls -l | grep ' ^[l]$argv[1]$ '

Thank you for any insight.
Edit: Using xtrace it says that variable name must start with a letter. I am guessing that the format in the $argv[1] is wrong, but I'm not sure. I've tried using quotes around it and single backquotes around it also.

Does it really have to be csh ?

unfortunately it does

ls -l | grep '^[l]'"$argv[1]"'$'

The shell sees 3 strings where the midle one is in " " i.e. allows variable substitution. grep sees one string without any quotes.

I'm not sure I understand. I tried what you wrote too, and it still says the variable must start with a letter.

Neither ls nor grep will complain about a variable name in this context. Please show us:

  1. the exact command line that you're using to invoke this script,
  2. the exact error message you're getting,
  3. the output from the command ls -l in the directory where you are running this script, and
  4. the output you were hoping to get out of the grep command.

I am getting no error messages now, but I am not getting the results that should come with the script.

For example, my script name is softLinkFinder.txt
I type in softLinkFinder.txt softlink.txt for the command.
With my script, I am trying to get what soft links are to the softlink.txt file. I am trying to do this by creating the script that is using grep to find from ls -l what lines start with an l and end with softlink.txt
When I type in ls -l I get:

tdrwxr-xr-x 2 pallen user 3864 Nov  3 15:49 bin
drwxr-xr-x 2 pallen user 3864 Oct 28 12:36 cpfolder
-rwxrwxrwx 1 pallen user  47 Oct 30 13:18 files.txt
lrwxrwxrwx 1 pallen user  29 Nov  3 13:11 softlink1.txt -> /home/p/pallen/softlink.txt
lrwxrwxrwx 1 pallen user   29 Nov  3 13:13 softlink2.txt -> /home/p/pallen/softlink.txt
lrwxrwxrwx 1 pallen user   29 Nov  3 13:13 softlink3.txt -> /home/p/pallen/softlink.txt
lrwxrwxrwx 1 pallen user   29 Nov  3 13:13 softlink4.txt -> /home/p/pallen/softlink.txt
lrwxrwxrwx 1 pallen user   29 Nov  3 13:13 softlink5.txt -> /home/p/pallen/softlink.txt
-rw-r--r-- 1 pallen user   55 Nov  3 09:11 softlink.txt

my script is a variation of:

#!/bin/csh
if ($#argv != 1) then
 echo "You have the wrong number of arguments."
endif
ls -l | grep '^[l] "$argv[1]"$'

I haven't gotten my else set up yet, but when I run the script, grep finds nothing. I am wanting to find the lines that start with l and end with the name of the argument. I think that was all. Thanks

You must allow some characters in between

ls -l | grep '^[l].*'"$argv[1]"'$'

Or shorter

ls -l | grep '^l.*'"$1"'$'

You need to allow some characters in between.

 ls -l | grep '^l.*softlink.txt' 

The "." matches any single character and "*" means that the preceding item, any character, will be matched zero or more times.

sammythesp3rmy,
The argument you passed to grep will only match the line:

l "$argv1"

There is a big difference between single quotes and double quotes, and double quotes inside single quotes are taken as literal characters.

I got it to work by using MadeInGermany's suggestion. Would you be able to explain what happened so I can understand? What do the second pair of single quotes around "$argv[1]" actually help it do to work? When you say the single quotes around that help make it a literal character, that the single quotes actually make what's inside the parenthesis an actual string, while argv[1] gets put into softlink.txt?

Not around the "$argv[1]" .
For the shell it is a concatenation of three strings

'string1'"string2"'string3'