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

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.

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

How to fix command not found in Linux shell scripting [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
why subtraction return - symbol
(2 answers)
Closed 2 years ago.
I have a Linux shell script with the below code
#! /bin/bash
echo $BASH
name = Mark
echo $name
When I run the script, I'm getting an error:
./my_script.sh: line 3: =: command not found
What am I doing wrong?
Note: I'm using Kali Linux.
In shell, you need to write:
echo $BASH
name=Mark
echo $name
Note there are no spaces around = when setting a variable. The shell usually interprets name = Mark as calling the command name with arguments =and Mark, hardly what you intend. It also seems that name somehow expands to nothing (an alias?), thus the confusing message about command =.

Is there a way to pipe the output of real users into the last command? [duplicate]

This question already has answers here:
How to apply shell command to each line of a command output?
(9 answers)
Running a command for each line of a text file (Bash)
(2 answers)
Easy shell solution to execute a command for each line of stdout [duplicate]
(3 answers)
How to process each output line in a loop?
(7 answers)
Closed 3 years ago.
I am trying to figure out a way to take the output of the list of users and pipe it into the last command directly.
I ran the following command initially and I get the list of all real users.
$ getent passwd {1000..60000} | cut -d: -f1
fly
pig
cow
How do I redirect that output into the last command so that I can display their login times?

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

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)

Reading values from config file in linux [duplicate]

This question already has answers here:
Need bash shell script for reading name value pairs from a file
(8 answers)
Reading key/value parameters from a file into a shell script
(1 answer)
How do I grab an INI value within a shell script?
(32 answers)
*export* all variables from key=value file to shell
(1 answer)
Closed 5 years ago.
I am working in linux and have a config file with a lot of single lines formatted like this:
Variable1=Value1
Variable2=Value2
Variable3=Value3
I need something I can run on command line that will echo the value for the respective variable. I have been playing with sed all day, but having a heck of a time. I'm not sure if that's even the best way. Any help would be super.
$ cat a.sh
Variable1=Value1
Variable2=Value2
Variable3=Value3
$ source a.sh
$ echo "$Variable1"
Value1
Note, source will overwrite the value of Variable1 for the current shell.
Search for the variable name and the equal sign, remove them, and print the result.
$ sed -n '/^Variable1=/{s/^Variable1=//;p}' config.txt
Value1

Resources