Bash function to scp a file not working
Solution 1
Not that it should matter, but the remote path should be /home/username
(single forward slash). And as sputnick pointed out, quote your ${1}
with "${1}"
.
I've copied the same command and it works when I test it, so I suspect (given the "not a regular file" error) that you have an extra space between [email protected]:
and //home/username
.
Another thing to try is to add debugging (by supplying -v
on the scp command) and see
if that gives any clues:
function to_company()
{
scp -v "${1}" [email protected]:/home/username
}
Solution 2
You're approaching this the wrong way. Instead of writing a shell wrapper, define a host alias in your ~/.ssh/config
.
Host work
Hostname long.server.name.company.com
User bob
Then, to copy a file to your company's server:
scp filename.ext work:
The remote path is relative to your home directory, so work:
means the same thing as work:/home/username
.
Your function fails if you pass an argument that contains whitespace or globbing characters \[?*
. That's easily fixed by putting the parameter expansion in double quotes: "$1"
. Always put double quotes around variable and command substitutions.
Another potential problem is that //
at the beginning of a path may be interpreted specially. Normally consecutive slashes in a path are equivalent to one slash, but a path that begins with exactly two slashes has a special meaning on some systems.
Related videos on Youtube
Bhaskar
Updated on September 18, 2022Comments
-
Bhaskar over 1 year
I wrote the following bash function:
function to_company() { scp ${1} [email protected]://home/username }
When I do:
$ to_company code_diff.txt
It asks for password and then fails with following message:
scp: //home/username: not a regular file
I tried giving
//home/username/
&//home/username/${1}
in the script but got the same result.If I manually execute above command and pass
code_diff.txt
in place of${1}
, file is transferred without issues.What mistake am I making?
-
Gilles Quenot almost 11 yearsAlways quote your variables in shell !
-
Bhaskar almost 11 years@sputnick, I changed the variable to
"$1"
instead of${1}
, but got the same result. -
slm almost 11 yearsI just tried this and it worked for me. I would drop the
//home
and make it/home
, not that I think it matters, but it might. Do you have read access on the file you're attempting to copy? -
ott-- almost 11 yearsFor testing, can you add a
echo
before your scp and check the output? -
Bhaskar almost 11 years@slm, @Drav Sloan, using
/home
instead of//home
worked for me. Thank you. -
slm almost 11 yearsGlad that was the issue. In general I usually use a tilde (
~
) such as this:scp file user@mach:~/some/dir/
.
-
-
Drav Sloan almost 11 yearsAnother thought is that you have
scp
aliased, what doestype scp
say? -
Bhaskar almost 11 yearsSingle forward slash solved. Thank you. How do I add debugging? By putting in echo statements?
-
Drav Sloan almost 11 yearsSorry I didnt make that clear, use
-v
on the scp (I'll make that clearer in my answer). -
Bhaskar almost 11 yearsWould you know why using double slash in script/function fails but when executing it manually works fine?
-
Drav Sloan almost 11 yearsI'm surprised it does to be honest, does the
-v
on the scp shed any light on the problem? -
Bhaskar almost 11 yearsMy script is now working with double slash and with single slash as well. I used
-v
to trace root cause but never hit the same error again. I am using GNU bash 3.2.48(1)-release (x86_64-apple-darwin12). I really don't know why it started working for both slashes. -
slm almost 11 years@Bhaskar - when I tried this the double slashes worked for me too. In Unix you can usually specify paths using multiple slashes such as
/home///sam
. They are just ignored. -
pretzlstyle over 2 yearsExcellent, this is the solution I needed; much better than mucking around with bash aliases. I'll note though, that this solution didn't work for me as-is; for whatever reason, I had to change
Username
toUser
, as for me,Username
was not a recognized ssh configuration option.