0

I was making a small script to get the ip address of the $SSH_CLIENT.

#!/bin/bash
set -euf -o pipefail
ip=${SSH_CLIENT%% *}
echo $ip

I keep getting an error with these two methods:

ip=echo $SSH_CLIENT | awk '{ print $1}'
 ip=echo $SSH_CONNECTION | awk '{print $1}'

ERROR:

$ ./ignoreip.sh
./ignoreip.sh: line 3: the.ip.address.of.ssh_client: command not found

-> changed the ip address for obvious reasons.

But it works with :

ip=${SSH_CLIENT%% *}

Can anyone explain to me why the two commands do not work but the last one does ?

  • Did you try just a plain `ip=$(echo $SSH_CLIENT)`? – Rogue Jul 12 '22 at 13:32
  • 1
    Did you mean to use Parens, Rogue? – Paul Hodges Jul 12 '22 at 13:35
  • @PaulHodges whoops, you're right. Been a hot minute since I've bash'd. – Rogue Jul 12 '22 at 13:37
  • 1
    BTW, `set -e` is [generally a bad idea](https://mywiki.wooledge.org/BashFAQ/105). The default behavior means you need to do a lot of manual error handling, but writing a bunch of error-handling code by hand is far better than enabling `set -e`'s highly version-dependent, [nonportable](https://www.in-ulm.de/~mascheck/various/set-e/), and hard-to-predict behavior. (If nothing else, at least browse the "exercises" section of BashFAQ #105 -- the first link in this comment). – Charles Duffy Jul 12 '22 at 13:52
  • (`set -u` [also has side effects that are often unwanted](https://mywiki.wooledge.org/BashFAQ/112), but it's much more defensible than `-e` is; nonetheless, I generally recommend static checking for use of unassigned variables with http://shellcheck.net/ as a better practice than `set -u`). – Charles Duffy Jul 12 '22 at 13:56

1 Answers1

0

Because your syntax is all wrong. Always try your code at https://www.shellcheck.net/ when something isn't making sense.

Line 3:
ip=echo $SSH_CLIENT
^-- SC2209 (warning): Use var=$(command) to assign output (or quote to assign string).

What you are doing is assigning echo to ip for the duration of the command (which then fails) and then attempting to execute the command in $SSH_CLIENT, which is apparently the.ip.address.of.ssh_client, which is not a valid command.

What you probably want is exactly what you posted at the top, but for purposes of examples...

ip=`echo "$SSH_CLIENT" | awk '{ print $1}'`    # old-style backticks
ip=$(echo "$SSH_CLIENT" | awk '{ print $1}')   # nestable bash subcall

There are a lot of other possibilities, but one of these is likely the kind of thing you were trying to do. Much better is what you ended up with, though -

ip="${SSH_CLIENT% *}"

or even

ip="${SSH_CLIENT/ */}"

Seriously, take the time to read through the whole manual, but especially Parameter parsing, and check out BashFAQ, especially (for this example) his page on subshells.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • 1
    Should probably demonstrate `echo "$SSH_CLIENT"` with the quotes in the awk examples, to avoid guiding folks into the bugs discussed in [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) – Charles Duffy Jul 12 '22 at 13:57