Lewis Denny
Lewis Denny

Reputation: 669

how to list file names into a function

I have a folder with a bunch of files, the files only have a url in it i.e

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

Here is my code. How can I get it to do this for each url in each file?

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

Upvotes: 0

Views: 86

Answers (2)

use a for loop

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

og if you prefer to give the filenames as arguments to the script then:

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

then run your script followed by the files

./script.sh folder/*

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272687

You could do:

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

Or even just:

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

Upvotes: 0

Related Questions