0

I'm a novice at bash (more like less than a novice). Trying to get the results of this into a variable

ssh root@HOST "ls -tr /path/to/files/Backups | tail -1"

This is what I have

#!/bin/bash
STR=ssh root@HOST "ls -tr /path/to/files/Backups | tail -1"
echo $STR

It returns myscript.sh: command not found

Script is located in the ~/Desktop/scripts I am running it in that directory by typing test.sh

How do I format the line for the variable?

Eric
  • 103
  • 1
    It is usualy a bad practice to use ls to get filenames from a script ; here you mention a backup path I guess you have dates or order number in the filename itself this sould be a better method to select your file names – francois P Jan 10 '20 at 16:31
  • 1
    Edit question to show: Where have you stored the script? How are you running it? – ctrl-alt-delor Jan 10 '20 at 16:40
  • 1
    The issue is most likely that they are not pointing to the path of the script (./myscript.sh) otherwise the error would be ./myscript.sh: command not found – jesse_b Jan 10 '20 at 16:41
  • 1
    Thanks to all for the suggestions. @francoisP thanks for the suggestion I may go another way now. – Eric Jan 10 '20 at 16:48
  • 1
    @francoisP I don't see any problem with what OP is trying to do. Just the original problem noted by the OP. And the script not being found, because not being called properly. – ctrl-alt-delor Jan 10 '20 at 17:00
  • 1
    Also note variable name should be lower case. And scripts should not end with .sh. There should be no file-name-extension. – ctrl-alt-delor Jan 10 '20 at 17:02

1 Answers1

3

str="$(ssh root@HOST "ls -tr /path/to/files/Backups | tail -1")"

to assign the result to the variable. However that is not the error that you are currently on. You are having trouble running your script. Use an absolute or relative path. e.g. ./my-script