[SOLVED] #!bin/sh script: variable in remote command
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm writing a script where I'm trying to place a variable in a remote command, but it's not working for some reason.
The part i'm having an issue with:
Code:
#!/bin/sh
#get root variables
. /root/.bashrc
remoteFileToCheck="/some/folder/on/remote/machine/file name with space.xls"
remoteAccessTime=`ssh <MyRemoteServerName> 'stat -c "%X" "${remoteFileToCheck}"'`
Where <MyRemoteServerName> is the hostname of my server.
The error on the last line is: cannot stat '': No such file or directory
When I replace ${remoteFileToCheck} with /some/folder/on/remote/machine/file name with space.xls it works just fine.
When I echo ${remoteFileToCheck} I get the correct filepath and name.
Things I've tried:
1. replace the backticks with $()
2. remove the single quotes (') around stat, but then it seems to split up the filename on the space
3. do `stat -c '%X' "${remoteFileToCheck}"` with remoteFileToCheck being a local file and this works fine.
So it appears to be a quoting problem, but I can't seem to find the correct way to quote in order to have it work.
Last edited by RagingRaven; 04-04-2017 at 03:22 AM.
Yes, it's a quoting problem. The outermost quotes must be double quotes so that the variables are processed by the shell. Then as you work your way through you'll have to use either single quotes or escaped double quotes.
As a style change, there are a lot of advantages to $( ) for command substitution instead of backticks.
Last but not least I'd recommend *not* doing this as root.
Last but not least I'd recommend *not* doing this as root.
I'm not actually executing the script as root, but as a sudo user, I just need the varables of the root user for some other part of the actual script.
Also the connection to the other machine is using a vpn, so there shouldn't be too much of an issue anyway.
I'm not actually executing the script as root, but as a sudo user, I just need the varables of the root user for some other part of the actual script.
Glad it runs. I'd recommend copying the variables you need from the root directory and inserting them into the beginning of your script. That way it won't have to run as root. If you add the following line to your script, it will certainly output "root" if I read your script and guess at the context correctly:
Code:
whoami
sudo is useful enough that I'd recommend the following detailed presentation:
Thank you for the read turbo, I already knew most of the things said in the presentation though.
The reason I'm using the sudo way, is because I need to run some programs with root privileges which use the variables of root.
Yes I could copy the variables to the sudo user, but I would have to do this each time a change is made to the root variables.
Further more there are some files on the remote machine which are only writable by root, yes I could add a user to a group with the right privileges, but I would also have to do this on the remote machine and keeping everything synced would be pretty cumbersome.
As said the connection between both machines is using a VPN, so the security risks should be minimal.
So it's more of a risk vs time assessment.
That said, it's always good to learn more and there were a few things in there I didn't know yet, so thanks!
Last edited by RagingRaven; 04-05-2017 at 02:47 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.