user2537388
user2537388

Reputation: 50

bash script + grep + perl commands

I'm working on a bash script to grep a file then to run a perl command. I know the commands work since I have been using them, but I can't seem to get them to work for the bash script. I would appreciate any help.

When I output $1 and so on it has the values, but when I output the grep command with them in it. I get file can't be found or blank spaces.

#! /bin/bash
usage()
{
echo "Usage:  $0"
echo $'\a'Supply 4 arguments
echo $0 arg1 arg2 arg3 arg4
echo "1. search parameters for bookmarks"
echo "2. What file to grep" 
echo "3. file to temporaly store results" 
echo "4. what file to store the final version"
exit 1
}

 main()
{
#   if [ "$#" -lt "4" ]
#   then
#       usage
#   else
echo "beginning grep"

grep -ir "$1" "$2" >> "$3"

echo "grep complete"

#   echo "beginning perl command"

#   perl -0ne 'print "$2\n$1\n" while (/a href=\"(.*?)\">(.*?)<\/a>/igs)' "$3" >> "$4"

#   echo "perl command complete"

#   echo "done"

#   exit 1
#   fi
}
main
echo $0
echo $1
echo $2
echo $3
echo $4

Upvotes: 0

Views: 441

Answers (1)

ooga
ooga

Reputation: 15501

Remember that when a bash function is called, the positional parameters are temporarily replaced with the function's parameters. So either don't make your mainline a function or pass your main function the input parameters. To pass the script's parameters to your main function, do this:

main "$@"

Upvotes: 2

Related Questions