Unable to print the public ip in shell script [duplicate] - linux

This question already has answers here:
What is the difference between $(command) and `command` in shell programming?
(6 answers)
Closed 4 years ago.
Hi I am trying to print the public ip of the machine in a file using shell script. I am using the command
ip=${curl ipinfo.io/ip}
in my script file and it gives an error saying bad substitution. whereas this command works when i run it in command line.
Is this the right way to get the ip through the script?
Thanks in advance!!

You are capturing the result of program so you should use $(). The following should work for you (with the -s parameter to curl stopping unnecessary output)
ip=$(curl -s ipinfo.io/ip)

Related

How to execute text from stdout in bash [duplicate]

This question already has answers here:
Bash script - variable content as a command to run
(7 answers)
Closed 1 year ago.
So I have a command that's like
cmd1|cmd2|...|cmdN|execute
The output of cmdN is one line that I want execute to execute as if I copy pasted the output of cmdN into the terminal myself. I've tried to replace execute with bash, with $, and I tried to use xargs. (I'm still kinda confused on each of the options I've tried).
What's the simple answer here that's gonna make me wanna delete this post?
If you need to execute it in the current shell, use the eval command:
eval "$(cmd1|cmd2|...|cmdN)"
If it can be executed in a subshell, pipe to bash:
cmd1|cmd2|...|cmdN | bash

Why Linux shell command is right in comand line, but it's error in shell file? [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 2 years ago.
"paste -d'|' <(echo 22)
the code is righ in command line , but I wrote it to a shell file , it's error.
this result :
How to resolve this!
Thinks!
#!/bin/bash informs the shell to use bash, and when you run it with sh it does not use bash as sh is forced.
sh is way more limited than bash, so if your script uses bash logic, but is run via sh, it is not fully compatible, and errors out where sh does not understand the commands.

How to fetch output of command into a variable? [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
I am trying to run a command and storing the values in a list
list = `sed -n 's/^abc//p' /etc/filename`
I am getting an error command not found while running the above command.
However, when I directly run the sed -n 's/^abc//p' /etc/filename command, the output is coming fine as below:
abc01 abc02 abc03
Use
list="$(sed -n 's/^abc//p' /etc/filename)"
There must be no spaces after variable declaration and equals sign. Also, quoting your variables is important.

Question about the meaning of backtick and %string [duplicate]

This question already has answers here:
What is the benefit of using $() instead of backticks in shell scripts? [duplicate]
(9 answers)
YYYY-MM-DD format date in shell script
(17 answers)
Closed 4 years ago.
I am learning bash script and I am reading some sample code.
Anyone can advice me the detail and meaning of the following code? Thanks in advance!
END_DATE=`date -u "+%FT%TZ" `
The backticks are performing Command Sbustitution. The command inside the backticks is executed in a subshell and the result is assigned to the variable. (another syntax that would work the same in bash is $(command))
The string for the date command is how format is specified for the date command.

How can I pipe bash output to a file and to the terminal at the same time? [duplicate]

This question already has answers here:
How do I write standard error to a file while using "tee" with a pipe?
(12 answers)
Closed 6 years ago.
This is a really basic question, but I couldn't find it asked this basically in an existing question, so please flag this if it's been answered before.
I want to pipe stderr from a bash command to a file, but not prevent it from being displayed on the terminal during execution.
More specifically, I have this .sh file:
nslookup MadeUpName
nslookup MadeUpName2
nslookup MadeUpName3
and I'm doing this:
. ./myScript.sh 2>errors.txt
This works to pipe error messages to errors.txt, but now I can't see the errors in the terminal as they happen.
You can use tee for displaying error and writing to an error file:
myScript.sh 2> >(tee error.log; exit)
If you want further information about this technique, see this page.

Resources