简体   繁体   中英

How can I refactor this bash script?

exportsData=$(curl -X GET \
 -H 'Accept: application/vnd.xxxxx+json; version=3.0' \
 -H 'Authorization: Bearer RCexxxxxxxxxxxxxxxxxxxxxxxxxcVefI5mWy' \
 -H 'Content-Type: application/json' \
https://api.xxxx.com/apps/9xxxxxxxxx15f96fe/exports)
status=$(echo $exportsData | jq '.[0].status')
downloadURL=$(echo $exportsData | jq '.[0].download_url') 
export ENCRYPTED_AES_KEY=$(echo $exportsData | jq '.[0].encrypted_aes_key')
export AES_IV=$(echo $exportsData | jq '.[0].aes_iv')

export ENCRYPTED_TARBALL=encryptedChatDump.tar.gz.enc
$(curl -X GET -o ENCRYPTED_TARBALL \
 -H 'Accept: application/vnd.xxxxx+json; version=3.0' \
 -H 'Authorization: Bearer RCexxxxxxxxxxxxxxxxxxxxxxxxxcVefI5mWy' \
 -H 'Content-Type: application/json' \
https://storage.googleapis.com/someUrlWhereTheFileIsBeingDownloadedFrom)

export OUTPUT_TAR=finalChatDumpUnencrypted.tar.gz
export PRIVATE_KEY_PATH=~/.ssh/id_rsa


openssl enc -in $ENCRYPTED_TARBALL -out $OUTPUT_TAR -d -aes-256-cbc |   base64 --decode | openssl rsautl -decrypt -inkey $PRIVATE_KEY_PATH | base64   --decode 

This is my first script and I am having a hard time writing the shortest possible code.

you could process the variables in a loop:

for key in status download_url encrypted_aes_key aes_iv
do
    val=$(echo "${exportsData}" | jq ".[0].${key}")
    echo "${val}"
done

EDIT:

in order to store the variables for further "processing", you might create them explicitly in the do loop as:

for key in status download_url encrypted_aes_key aes_iv
do
    val=$(echo "${exportsData}" | jq ".[0].${key}")
    declare -x "$(echo ${key} | tr '[a-z]' '[A-Z]')"="${val}"
done

this will create (and export) upper-cased variables STATUS , DOWNLOAD_URL , etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM