0

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

1 Answer 1

2

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 "$@"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.