2014-09-04 168 views
0

我正在使用bash腳本來grep文件,然後運行perl命令。我知道這些命令自從我使用它們以來就工作了,但我似乎無法讓它們爲bash腳本工作。我將不勝感激任何幫助。bash腳本+ grep + perl命令

當我輸出$ 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 

回答

2

請記住,當調用bash函數時,位置參數會暫時替換爲函數的參數。因此,要麼不要讓你的主線功能,要麼通過輸入參數傳遞你的功能。要將腳本參數傳遞到main函數,請執行以下操作:

main "[email protected]"