bash脚本+ grep + perl命令

时间:2014-09-04 20:16:24

标签: bash perl grep

我正在使用bash脚本来grep文件然后运行perl命令。我知道这些命令在我使用它们之后起作用,但我似乎无法让它们为bash脚本工作。我将不胜感激任何帮助。

当我输出$ 1等等时,它有值,但是当我输出grep命令时,它就会输出。我找不到文件或空格。

#! /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 个答案:

答案 0 :(得分:2)

请记住,当调用bash函数时,位置参数会暂时替换为函数的参数。因此,要么不使主线成为函数,要么将main函数传递给输入参数。要将脚本的参数传递给main函数,请执行以下操作:

main "$@"