0

I have been at this most of the morning but had to work, so I am back and hoping somebody can help me figure this out.

This line works

 dest="$user@$dest:$( hostname )"
 # dest=me@remotehost.com:MyMachineHostname

This like I am really struggling with, I want to be able to run this on several hosts just by uploading the file and setting a cron job.

dir=$(( $unique == 1 ? ":"$( hostname ) : "" ))
dest="$user@$dest$dir"

When unique == 1 then the colon and hostname could be set to $dir, if not it should be empty.

This is definitely not PHP, so much harder.

I have tried many variations of brackets, braces without any without $ The closest I got was having :myhostname but with lots of the other chars around it.

It's very frustrating.

CodingInTheUK
  • 930
  • 7
  • 16
  • 2
    Ternary operators only work in an arithmetic context. Everything they include needs to be integer math. You can't use them to substitute strings. – Charles Duffy Aug 24 '21 at 23:37
  • `dir=$( (( unique )) && hostname )` would be a way to implement what you're asking for, though it's a little bit icky from a performance perspective (but then, so is the `hostname` command) – Charles Duffy Aug 24 '21 at 23:38
  • ... a better practice approach would just be `if (( unique )); then dir=$HOSTNAME; fi`. No ternary needed, and using the built-in variable `$HOSTNAME` is _far_ faster than `$(hostname)` (command substitutions are universally slow; so is spinning up an external command). – Charles Duffy Aug 24 '21 at 23:39
  • BTW, what's this "date" in the title? The question body doesn't say anything about dates at all. – Charles Duffy Aug 24 '21 at 23:41
  • Thank you for the replies, sorry about the confusing date part, its was basically the same as hostname except running a date function, I realised while writing if one was working I could do the other the same name, hostname was shorter without format etc so I opted to ask about that one instead. Glad I did, I have some good answers to go and some code impovement at the same time in the comments. I will update the title, now. sorry about the confusion. – CodingInTheUK Aug 24 '21 at 23:52
  • Also, `dir=":$(hostname)"` -- the `$(hostname)` should be inside the double quotes, whereas the `:` actually doesn't need to be (`dir=:"$(hostname)"` would have the exact same meaning). Not actually important for an assignment to a string -- that context suppresses glob expansion and string-splitting regardless -- but important in many/most other contexts in POSIX-flavored shell languages, so it's an important habit to be in. – Charles Duffy Aug 24 '21 at 23:53
  • See [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) for a taste of the kinds of bugs that not quoting your expansions leads to (and `$(hostname)` is just as much an expansion as `$variable` is). – Charles Duffy Aug 24 '21 at 23:56
  • Thank you Charles, I will take a look into that. As you can tell bash is new to me. All I am doing is making a small back up script. I could have done it with rsync and crontab I know, but I wanted gnome notifications hence the small script. I any case, you have been very helpful. I will be referring back to this question I am sure. – CodingInTheUK Aug 24 '21 at 23:58

1 Answers1

1
if (( unique )); then
  dir=:$(hostname)
fi
dest="$user@$dest$dir"
user1984
  • 5,990
  • 2
  • 13
  • 32
  • If you're going to use bash-only syntax, why `[[ ]]` instead of `(( ))` when the context is arithmetic? `if (( unique )); then` is shorter and easier to read, or `if (( unique == 1 ))` if you want to keep parity and treat all other positive integers as false. – Charles Duffy Aug 24 '21 at 23:41
  • `(( ))` to make it an arithmetic comparison, right? – user1984 Aug 24 '21 at 23:42
  • Right, `(( ))` enters an arithmetic context, so `(( unique ))` is evaluating `unique` using arithmetic rules, which means that 0 is false, any positive number is true; and when used just as above, an empty string is treated as zero. (Whereas adding a `$` as in `if (( $unique )); then` would treat an empty string as an error, instead of treating it as zero/false). – Charles Duffy Aug 24 '21 at 23:43
  • I fully agree with you. I'm not so proficient and wanted to keep it as vanilla as possible. `if (( unique )); then` it is. – user1984 Aug 24 '21 at 23:43
  • `(( ))` already does the variable expansion, right? we don't need the `$` inside. @CharlesDuffy – user1984 Aug 24 '21 at 23:45
  • 1
    Right, but the rules are different for the two cases. `(( somevar ))` does indeed expand `somevar`, but it expands it in a way that involves trying harder than usual to find a way to treat it as a number. Whereas `(( $somevar ))` requires whatever is in `somevar` to already _be_ a valid integer for it to work without throwing an error. – Charles Duffy Aug 24 '21 at 23:46