0

I have store ip address with port in a file and I want to read it using shell script. Thus file serverIP has data 192.168.1.17:3000. I am using following bash script to read it

IPAddressFile=/home/geo/serverIP
SERVER_IP_PORT=$(<$IPAddressFile)
echo $SERVER_IP_PORT

But this script echo empty string. Where I am making mistake?

prattom
  • 1,625
  • 11
  • 42
  • 67
  • 1
    Can't replicate with the commands you show. Did you copy-paste the script into the question? If not, is everything spelled correctly in the script? – Some programmer dude Jan 15 '19 at 13:01
  • Possible duplicate. Please refer to this anser: [How to read a file into a variable in shell?](https://stackoverflow.com/a/10771857) – brunorey Jan 15 '19 at 13:05
  • No I am following the same code in given accepted answer but it's not working my case and that's why I have posted this question – prattom Jan 15 '19 at 13:08
  • 2
    How are you running the script? If you run `sh yourscript` it won't have bash-only extensions such as `$(<...)`; it *must* be invoked with bash as the shell. – Charles Duffy Jan 15 '19 at 13:09
  • Yes I am running `sh scriptfile` – prattom Jan 15 '19 at 13:09
  • 2
    Don't. If you want to use bash syntax, you must `bash scriptfile`, or set it executable with a `#!/bin/bash` shebang and run `./scriptfile`. – Charles Duffy Jan 15 '19 at 13:09
  • Thanks Charles this is the problem. – prattom Jan 15 '19 at 13:10

1 Answers1

2

If you're going to use bash-only syntax like $(<...), your script must be run with bash, not sh.

Thus, either run bash yourscript or add a #!/bin/bash (or similar) shebang, flag the file executable, and invoke it as a command, for example ./yourscript


As an alternative that's both efficient and compatible with POSIX sh:

IFS= read -r SERVER_IP_PORT <"$IPAddressFile"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • What? Charles Duffy is not using `"${IPAddressFile}"`? – hek2mgl Jan 15 '19 at 13:12
  • @hek2mgl, ...when did I ever support use of braces in contexts where they're not necessary for correctness or clarity? You must be thinking of someone else. :) – Charles Duffy Jan 15 '19 at 13:13
  • hihi. didn't know that this is your opinion on that :) I mean yeah, they are unnecessary here (which required _me_ a test), but isn't it a good practice to always use them? (It's also good for the muscles of your fingers btw, at least on a German keyboard) – hek2mgl Jan 15 '19 at 13:16
  • 1
    ...now, what *is* unlike me is failing to lecture the OP about their use of all-caps names for self-defined variables :) – Charles Duffy Jan 15 '19 at 13:16
  • ...but re: "good practice to always use them", there's not a prevailing opinion either way that I'm aware of. Sometimes they *must* be used -- if you're concatenating an expansion with trailing characters that are valid in variable names; or if you want to parameterize your expansion -- but otherwise it's a style choice. – Charles Duffy Jan 15 '19 at 13:17
  • 1
    I was thinking that when people just consequently use them, they will never have to spent time thinking about if they have to use them, or forget them. And it gives a cleaner overall picture of the code. – hek2mgl Jan 15 '19 at 13:21
  • 1
    *nod*, that's a very defensible position; I certainly won't say you're wrong. OTOH, most of the shell scripts I write outside Stack Overflow these days are being embedded in [Nix](https://nixos.org/nix/) code, and Nix uses `${...}` as an escape to embed a Nix expression in a string unless escaped as `''${...}` -- so it'd be a bit painful to follow that convention in my real-world day-to-day. – Charles Duffy Jan 15 '19 at 13:26