Add braces to a command

wallis

New Member
Joined
May 15, 2024
Messages
4
Reaction score
1
Credits
52
I want to automate a scp command like the following:
scp [email protected]:/home/pi/remote/\{aaainremote,bbbinremote,cccinremote\} /home/pi/local
The above command works as expected when entered in a terminal.



The following is a condensed version of my bash script named cwmscp:
The variables are named as arguments in the full script, but simplified here as shown.
filename1=“aaainremote”
filename2=“bbbinremote”
file names=“cccinremote”
ipaddress=“192.168.1.100”
source=“/home/pi/remote/“
destination=“/homepi/local”
scp pi@$ipaddress:$source\{filename1,filename2,filename3\} $destination

When I execute the bash script, I get the password prompt for the remote system and I enter the password.
The first file, aaainremote is copied, but not the bbbinremote or cccinremote.
I can change the order of the three file names and only the first file named is copied

I get the the following output:

[email protected]’s password: (I enter the password)
aaainremote. 100% 4. 6.9KB/s. 00:00
scp: /home/pi/remote: not a regular file
scp: /home/pi/remote: not a regular file

The three files are identical test files with different names:
Only the first file named in the scp command gets copied.


I can manually type the following in a terminal session and all three files get copied as expected:
scp [email protected]:/home/pi/remote/\{aaainremote,bbbinremote,cccinremote\} /home/pi/local
 


my understanding of the command syntax is that you can specify only one file not 2 or more.
scp pi@$ipaddress:$source\{filename1,filename2,filename3\} $destination
Local file
names can be made explicit using absolute or relative pathnames
to avoid treating file names containing ‘:’ as host specifiers.

Your syntax is contradictory to the quoted docs:
 
my understanding of the command syntax is that you can specify only one file not 2 or more.

As @wallis said, his code works as a terminal command. So, I would be asking the same question. Why not in a bash, if it works in terminal? It doesn't really make sense to me. And as I'm a newb, that's to be expected.
 
I got the syntax I used from some documentation, so I will do some more reading, but as @CaffeineAddict said, why does it work in a terminal command. I tried the scp in a terminal prompt again as follows and all three files are copied.
The terminal command I used that copies all 3 files is:
scp [email protected]:/home/pi/remote/\{aaainremote,bbbinremote,cccinremote\} /home/pi/local

I am new to bash scripting and would appreciate any help on why the scp command works from a terminal prompt and not when using variable names.

Thanks for all comments.
 
G'day @wallis and welcome to linux.org :)

I am moving this to Command Line where scripting inquiries are also handled.

Good luck

Chris Turner
wizardfromoz
 
These kind of commands can be very tricky to debug, but I would start with:

Why do you double quote all variables ?

filename1=“aaainremote”
filename2=“bbbinremote”
file names=“cccinremote”
ipaddress=“192.168.1.100”
source=“/home/pi/remote/“

Paths also don't need slash at the end
 
I want to automate a scp command like the following:
scp [email protected]:/home/pi/remote/\{aaainremote,bbbinremote,cccinremote\} /home/pi/local
The above command works as expected when entered in a terminal.



The following is a condensed version of my bash script named cwmscp:
The variables are named as arguments in the full script, but simplified here as shown.
filename1=“aaainremote”
filename2=“bbbinremote”
file names=“cccinremote”
ipaddress=“192.168.1.100”
source=“/home/pi/remote/“
destination=“/homepi/local”
scp pi@$ipaddress:$source\{filename1,filename2,filename3\} $destination

When I execute the bash script, I get the password prompt for the remote system and I enter the password.
The first file, aaainremote is copied, but not the bbbinremote or cccinremote.
I can change the order of the three file names and only the first file named is copied

I get the the following output:

[email protected]’s password: (I enter the password)
aaainremote. 100% 4. 6.9KB/s. 00:00
scp: /home/pi/remote: not a regular file
scp: /home/pi/remote: not a regular file

The three files are identical test files with different names:
Only the first file named in the scp command gets copied.


I can manually type the following in a terminal session and all three files get copied as expected:
scp [email protected]:/home/pi/remote/\{aaainremote,bbbinremote,cccinremote\} /home/pi/local
Why not specify the files with a glob since they all have a common element in the name in post #1.
For example, this works:

scp *remote <some-address>

The command will copy all the files with "remote" as the string on the end of their name to the designated address.
 
See question in code.
Bash:
filename1=“aaainremote”
filename2=“bbbinremote”
file names=“cccinremote”  # <--  What is this?  Is that supposed to be filename3?
ipaddress=“192.168.1.100”
source=“/home/pi/remote/“
destination=“/homepi/local”
scp pi@$ipaddress:$source\{filename1,filename2,filename3\} $destination

The next question is why are you making it a script like this if they are hard coded? You do it often?

Then do this without all the fancy variables that are already hard coded anyhow. (escape backslashes aren't necessary)
Bash:
#!/bin/bash 
scp /path/to/files/{filename1,filename2,filename3} user@host:/path/to/destination/
 
Why not specify the files with a glob since they all have a common element in the name in post #1.
For example, this works:

scp *remote <some-address>

The command will copy all the files with "remote" as the string on the end of their name to the designated address.

Well, that may be a problem, if it copies ALL the files ending with "remote"
You have the 3 files you want (if the respective sources exist), but there may be other files matching that name filter, and these do not necessarily need to be copied as well. Actually, they really may not be copied in some cases. Also, the other files - which don't need to be copied - may be very large, so you wait for ... nothing really. And wasted disk space. - But, in general, never just use * where you think it's OK. Non-specific filters are sources of many, many bugs. Always make wildcards as specific as possible. You can use multiple * and ? in any filename spec, something that also not everybody knows.
 

Members online


Top