Wrote a script, the main task of which is to upload and download files.
#!/bin/bash
fileUpload() {
local filepath=$1
local transfer_path=$2
local url
url=$(curl --progress-bar --upload-file "$filepath" "https://transfer.sh/$transfer_path")
echo "$url"
}
printOutUpload() {
local filepath=$1
local url
echo "Uploading $filepath"
url=$(fileUpload "$filepath")
echo "Transfer File URL: $url"
}
fileDownload() {
local destination=$1
local url=$2
local file_name=$3
curl -# "https://transfer.sh/$url/$file_name" -o "$destination/$file_name"
}
printOutDownload() {
if [ $? -eq 0 ]; then
echo "Success!"
else
echo "Error: There was a problem downloading the file."
fi
}
while getopts "d:" opt; do
case $opt in
d)
printOutDownload
fileDownload "$2" "$3" "$4"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
With flag -d script download single file from the transfer:
./script -d ./tests HJkv1I test.txt
Without it should upload files:
./script test.txt test1.txt
One by one, each of these functions work correctly, but I can't get them to work in one script - If I add the -d flag, downloading is working, but upload not, its do nothing
CodePudding user response:
Set a flag when processing the arguments. Then after the getopts
loop, use an if
statement to perform an upload or download depending on the flag.
direction=up
while getopts "d:" opt; do
case $opt in
d) direction=down
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
shift (($OPTIND-1))
if [[ $direction == up ]]
then
printOutUpload
fileUpload "$@"
else
printOutDownload
fileDownload "$@"
fi