I would like to write a bash script for transferring multiple directories from a Raspberry Pi to my laptop using scp command. But my code only work on one laptop (MacBook Pro 2016), but it failed on another laptop (MacBook Pro 2020), which is quite strange.
What I would like to execute is some command like:
scp -rp [email protected]:/home/pi/Documents/data/{directory1,directory2,directory3} .
This command work well when I directly execute it in terminal, on both of the laptop. But when I put it into a bash script:
#!/bin/bash
scp -rp "[email protected]:/home/pi/Documents/data/{directory1,directory2,directory3}" "."
it shows error scp: /home/pi/Documents/data/{directory1,directory2,directory3}: No such file or directory on my MacBook Pro 2020, but it works well on my MacBook Pro 2016.
I have checked the bash version on both laptop, but it seems like they don't have too much difference
MacBook Pro 2016: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin21)
MacBook Pro 2020: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin22)
Can anyone help me?
-------------------------------- Below are updates --------------------------------
Seem like I didn't explain my problem very well.
My actual situation is a litter more complicated. I am working on a bash script which takes multiple directory as parameter for doing scp from my Raspberry Pi remote. My script.sh is like:
#!/bin/bash
while getopts "hu:" option; do
case $option in
h) # display Help
Help
exit;;
u)
user_list+=("$OPTARG");;
\?) # incorrect option
echo "Error: Invalid option"
exit;;
esac
done
RASPBERRYPI_DATA_PATH="/home/pi/Documents/data"
RASPBERRYPI_ADDR=$1
RASPBERRYPI_USER_NAME="pi"
scp -rp ${RASPBERRYPI_USER_NAME}@${RASPBERRYPI_ADDR}:${RASPBERRYPI_DATA_PATH}/{${user_list[@]// /,}} .
I execute the script in this way:
./script.sh -u "directory1 directory2" 192.168.0.163
It shows error: scp: /home/pi/Documents/data/{directory1,directory2}: No such file or directory on my MacBook Pro 2020, but it works well on my MacBook Pro 2016.
I am a novice in bash development. I think I am having some mistake on grammar. But the script works well on my old MacBook Pro, which is quite strange.
Thank you very much for helping me.
This is due to the double quotes in your script. Remove them and it should work again.
Test with something like this:
output:
Why? Brace expansion in bash and zsh just work that way.