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.
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
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?