LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 04-04-2017, 03:21 AM   #1
RagingRaven
Member
 
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62

Rep: Reputation: Disabled
Question #!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.
 
Old 04-04-2017, 03:34 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
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.
 
Old 04-04-2017, 03:38 AM   #3
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
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.
 
Old 04-04-2017, 06:12 AM   #4
RagingRaven
Member
 
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-04-2017, 06:51 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Quote:
Originally Posted by RagingRaven View Post
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:

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.
 
Old 04-05-2017, 02:46 AM   #6
RagingRaven
Member
 
Registered: Sep 2011
Location: Oud-Beijerland, The Netherlands
Distribution: Mageia 4
Posts: 62

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply

Tags
command, remote, substitution, variable



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
variable from remote script to local script SuKHI Linux - Newbie 7 10-20-2015 06:53 PM
[SOLVED] Bash script: How to assign variable to an sqlite3 command with variable embedded? ninja6o4 Linux - Software 10 02-15-2015 04:43 PM
[Script] How to catch the output of a command to a variable? thomas2004ch Linux - Software 5 07-30-2012 12:56 AM
Declare variable in a script against a command shipon_97 Linux - Newbie 13 12-07-2007 03:17 AM
Script: put the output of a command into a variable. poincare999 Programming 4 11-04-2007 11:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration