— shell, code snippet, today-i-learned — 1 min read
Was writing a script to pull metadata from an NFT collection.
I had to write a loop that queried the API for the asset ID, which then generated JSON files in batches of 10, now prepopulated with asset IDs. These weren't in proper JSON format yet, so I had to add the array brackets at the start and end of the file. Also had to account for customization (in case we want to try querying for another NFT project).
One of the cool things I did was writing a shell script that used flagged params, like so: Ref
1while getopts n:p:a: flag2do3 case "${flag}" in4 n) max_drop=${OPTARG};;5 p) policy_id=${OPTARG};;6 a) auth=${OPTARG};;7 esac8done
So the command can be run as: ./script.sh -n 10000
or ./script.sh -p <POLICY_ID>
or ./script -n 4000 -p <POLICY_ID>
.
Some other code snippets I found useful:
1echo 'task goes here' | cat - todo.txt > temp && mv temp todo.txt23sed -i '1s/^/task goes here\n/' todo.txt45sed -i '1itask goes here' todo.txt
1sed -i '.bak' '1s/^/task goes here\'$'\n/g' todo.txt2echo -e "task goes here\n$(cat todo.txt)" > todo.txt3echo 'task goes here' | cat - todo.txt > temp && mv temp todo.txt
1printf '%s\n' '$' 's/.$//' wq | ex somefile
1expr $x / $y2echo $((x / y))3z = $((x / y))
1for i in $(eval echo {$1..$2}); do echo $i; done