Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
04-04-2017, 03:21 AM
|
#1
|
Member
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62
Rep: 
|
#!bin/sh script: variable in remote command
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.
|
|
|
04-04-2017, 03:34 AM
|
#2
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
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.
|
|
|
04-04-2017, 03:38 AM
|
#3
|
Senior Member
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375
|
Consider the differences here...
Code:
# myvar="test"
# echo $myvar
test
# `echo $myvar`
# 'echo $myvar'
bash: echo $myvar: command not found...
# "echo $myvar"
bash: echo test: command not found...
In this case, what you are probably after is $()
Code:
# myvar2=$(echo $myvar)
# echo $myvar2
test
Last edited by r3sistance; 04-04-2017 at 03:41 AM.
|
|
|
04-04-2017, 06:12 AM
|
#4
|
Member
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62
Original Poster
Rep: 
|
First off, thank you for your replies.
Quote:
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.
Lastly, I now changed the line to:
Code:
$(ssh <MyRemoteServerName> "stat -c '%X' '${remoteFileToCheck}'")
And it works!
So thanks again.
|
|
|
04-04-2017, 06:51 AM
|
#5
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
Quote:
Originally Posted by RagingRaven
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:
sudo is useful enough that I'd recommend the following detailed presentation:
He has a useful and concise book as well, if you enjoy paper better: sudo Mastery
With either one, you'll learn the more powerful capabilities and how to avoid pitfalls.
|
|
|
04-05-2017, 02:46 AM
|
#6
|
Member
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62
Original Poster
Rep: 
|
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.
|
|
|
All times are GMT -5. The time now is 08:39 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|