reading a line from file using shell script [duplicate] - bash

This question already has answers here:
How to read a file into a variable in shell?
(9 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
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?

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"

Related

Why we use #!/bin/bash in Bash script? [duplicate]

This question already has answers here:
Why do you need to put #!/bin/bash at the beginning of a script file?
(10 answers)
What is the preferred Bash shebang ("#!")?
(6 answers)
Closed 4 years ago.
What is the significant of using #!/bin/bash in the starting of bash script? Can we write a bash script without #!/bin/bash ?
This line is called shebang. It’s a ‚magic‘ line telling the program loader (kernel) how to execute a script on unixoid systems.
Cf. https://en.m.wikipedia.org/wiki/Shebang_(Unix)

Running a command line through shell script is not working [duplicate]

This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 4 years ago.
I have an application in Unix. I use the below command to connect to it:
./application -a "connect"
I want to do the same through the shell script, for which i assigned the command line to a variable like:
newcommand = './application -a "connect"'
$newcommand
But this is not working.
However the first part of the code is working. i.e.,:
newcommand = "./application"
$newcommand
Can anyone point out what i am missing.
Believe it or not, this:
newcommand = "./application"
...has the shell run the command, newcommand with the arguments, =, and ./application.
In shell simple assignments cannot have any unprotected whitespace or they'll be interpreted as a command.
Consider:
newcommand=./application
$newcommand
...notice that there's no space around the = sign in the assignment.

Is it mandatory to use #!/bin/bash even inside bash subshell [duplicate]

This question already has answers here:
Writing a Bash script without the shebang line
(2 answers)
Bash script execution with and without shebang in Linux and BSD
(2 answers)
Closed 5 years ago.
If after logging into my system I type: bash (to use bash subshell) and then try to run a bash script (e.g. example.sh), then does it matter if I do not put #!/bin/bash as the first line of the script or it is fine since I am already inside bash subshell?

Bash Script Requires Enter Before Any Input [duplicate]

This question already has answers here:
Script parameters in Bash
(5 answers)
Closed 6 years ago.
I've written a simple bash script, named ocropus:
#!/bin/bash
read filename path
...
And then i realized that I can't run it like this:
ocropus filename path
Instead, I need to run it like this:
ocropus
filename path
What can I do so I don't need to hit enter before my inputs? Thanks a lot!
Command line arguments are in $1, $2, etc. So do:
filename=$1
path=$2
instead of
read filename path

The function definitions in shell use dash [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 6 years ago.
The following code:
#!/bin/bash
function me-test()
{
echo 'test'
}
me-test
The execution method below is not correct:
#sh 1.sh
1.sh: line 6: `me-test': not a valid identifier
but the execution method below is correct:
#./1.sh
test
In other programming languages,it can not using dash to define function.For example,python.
why shell is so that?
sh 1.sh runs the script as a Bourne Shell script and ./1.sh runs it as a bash script because of your line #!/bin/bash (this is called a shebang which tells the shell which interpreter to use) calls the bash interpreter to run the script. bash allows for hyphens in function names but Bourne Shell does not. The two are very similar but different programming languages.
If you changed #!/bin/bash to #!/bin/sh you'd get an error every time you ran the program.

Resources