如何将文件名列入函数

时间:2011-12-18 13:15:58

标签: bash

我有一个包含大量文件的文件夹,文件中只有一个网址,即

http://itunes.apple.com/us/app/keynote/id361285480?mt=8

这是我的代码。如何为每个文件中的每个URL执行此操作?

var='{"object":"App","action":"scrape","args":{"itunes_url":"!!!!HERE!!!!"}}'          
string=$(echo "$var" | sed -e 's/"/\\"/g')
string='{"request":"'"$string"'"}'
api="http://api.lewis.com"
output=$(curl -s -d "request=$string" "$api")

code=$(echo "$output" | tr '{', '\n' | sed -n "2p" | sed -e 's/:/ /' | awk '{print $2}')
if [ "${code:0:1}" -ne "2" ]; then
    # :(
    echo "Error: response code $code was returned, "
else
    string=$(echo "$output" | tr '{', '\n' | sed -e '/"signature":\(.*\)/d;/"data":\(.*\)/d;/"signature":\(.*\)/d;/"code":\(.*\)/d' |sed -e 's/\\"//g;s/\\\\\\\//\//g;s/\\//g' | tr '}', '\n' | sed -e 's/"//' | sed '/^$/d')
    echo "$string"
fi

2 个答案:

答案 0 :(得分:1)

使用for循环

for filename in folder/*; do 
-- your code where you do something using $filename --
done

如果您希望将文件名作为参数提供给脚本,那么:

for filename do
-- your code where you do something using $filename --
done

然后运行脚本,然后运行文件

./script.sh folder/*

答案 1 :(得分:0)

你可以这样做:

for file in *; do
    for line in $(cat $file); do
        # Stuff goes here
    done
done

甚至只是:

for line in $(cat *); do
    # Stuff goes here
done