LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   write a bash script (https://www.linuxquestions.org/questions/linux-general-1/write-a-bash-script-4175660773/)

Pedroski 09-14-2019 06:51 AM

Well thank you both very much! I have something that works. I can always make it better later, perhaps.

@Firerat: I don't need to back up the files really, I changed my php file to write 2 copies to 2 directories. If, after a week or a month, no students are complaining about their results, I will delete all the backups anyway. Each week I need to get rid of the old files so they don't get mixed up with new, incoming files.

I'm afraid I don't understand anything about your tips for ssh/config I am just not a computer person.

I would like to ask about: ssh $server:${RemotePath}/ "${Answers}"

Why do I need the / and why the "" around ${Answers}?? And why do I need {}?

Firerat 09-14-2019 07:25 AM

Quote:

Originally Posted by Pedroski (Post 6036677)
I'm afraid I don't understand anything about your tips for ssh/config I am just not a computer person.

no problem, just use the long version "-e 'ssh -i ~/.ssh/id.rsa' "

Quote:

Originally Posted by Pedroski (Post 6036677)
I would like to ask about: ssh $server:${RemotePath}/ "${Answers}"

Why do I need the / and why the "" around ${Answers}?? And why do I need {}?

ok, the trailling / on ${RemotePath}/

rsync behaves differently if it ends /

Code:

server:path/to/remotedir /path/on/local/incoming
That creates
/path/on/local/incoming/remotedir
containing the contents

Code:

server:path/to/remotedir/ /path/on/local/incoming
That puts the contents into /path/on/local/incoming



The "" ,
this is to prevent splitting,
Answers="/path/to/dir with spaces"

rsync server:path/upload ${Answers}

would be seen by the shell like
'rsync server:path/upload' '/path/to/dir' 'with' 'spaces'

with "" around it
'rsync server:path/upload' '/path/to/dir with spaces'


{} easier if I use an example
Code:

#!/bin/bash
var=foo
echo this is foo ${var}
echo this is foo $var
# they will both work fine
echo this is foobar ${var}bar
# that one works
echo this is foobar $varbar
# that one doesn't ( only one foobar and not two )

You don't *need* them all the time
Personal I just think they look better so it is rare that I don't use them

Pedroski 09-14-2019 05:32 PM

Thanks again, you've been a great help!

As a non-computer person, I can only get some working code from somewhere, then slowly alter 1 little thing at a time, see what happens, till I get what I want. And of course, I ask for tips, here, or on Python or PHP forums.

I do that with Python. Now I have code which generates homework webpages and css files, collects and marks the homework and enters the grades in my records, all just from tiny building blocks and text files! I'm always amazed that it works!

Thanks a lot!

MadeInGermany 09-15-2019 04:20 AM

Quote:

Code:

echo this is foobar ${var}bar

$variables in command arguments (here: the echo command) are subject to unwanted expansions. Better have them in quotes, so the shell does nothing but substitutes them by their values:
Code:

echo "this is foobar ${var}bar"
Or
Code:

echo this is foobar "${var}bar"
Or
Code:

echo this is foobar "$var"bar
The latter separates $var from bar so a {} is not needed.

Firerat 09-15-2019 09:58 AM

echo was just a cheap way to show what {} was doing

assume the answers needed base dir

Code:

username@123.123.123.123:${RemotePath}/ "${base}/${Answers}"
username@123.123.123.123:${RemotePath}/ "$base"/"$Answers"

I know the first works, the second? I'm not sure


All times are GMT -5. The time now is 01:23 PM.