Bash script doesn't show output, but the exact same script in a oneliner does [duplicate] - bash

This question already has answers here:
Printing output of C program in shell script
(1 answer)
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 7 months ago.
This bash script returns nothing, but the exact same script ran in a oneliner returns the correct output.
#!/bin/bash
URL="localhost:3000"
content=$(curl -s $URL | sed -E 's/<[^>]*>//g')
vidsWatched=$(echo $content | grep -A 3 "Videos Watched" | sed -n 4p)
echo "$vidsWatched"
Vs the onliner:
curl -s "http://localhost:3000" | sed -E 's/<[^>]*>//g' | grep -A 3 "Videos Watched" | sed -n 4p
Output:
600

Related

use grep and cut command to get output of each line in next line [duplicate]

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 2 months ago.
$ cat hello.txt
shreenivasa hi hello this is test mail
shreenivasa how are you man
if i run in terminal
$ cat hello.txt | grep shreenivasa | cut -d ' ' -f 2-
its giving following output
hi hello this is test mail
how are you man
but if i write it as script in hello.sh like below
#!/bin/bash
hi=`cat hello.txt | grep shreenivasa | cut -d ' ' -f 2-`
echo $hi
output for ./hello.sh is
hi hello this is test mail how are you man.
I want to print "how are you man" in next line
like
hi hello this is test mail
how are you man
tried $hi\n didn't work

Can't assign linux command output into a variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I've tried to write the following code in bash but I am not able to get the output to be echoed.
part1="blkid | grep -P 'CENTOS 7' | cut -c1-9"
echo "$part1"
Try this
part1=$(blkid | grep -P 'CENTOS 7' | cut -c1-9)
echo "$part1"

bash substring from xargs piped [duplicate]

This question already has answers here:
How to process each output line in a loop?
(7 answers)
Closed 4 years ago.
Reading how to get substring in bash, I found out the following commands works:
var="/aaa/bbb/ccc/ddd"
echo ${var##*/}
Which produces "ddd"
My complete problem is to make this dinamically reading from a pipe:
I want to achieve something like this:
echo /aaa/bbb/ccc/ddd | xargs echo ${MyVar##*/}
But it is not working.
I tried to use -I option follwing way:
echo /aaa/bbb/ccc/ddd | xargs -I MyVar echo ${MyVar##*/}
And did not work either, (I think it does not interpolate it)
Any way to solve this?
Is it posible to achieve also to read substring left part, instead of right part?
You may use it like this:
echo '/aaa/bbb/ccc/ddd' | xargs -I {} bash -c 'echo "${1##*/}"' - {}
ddd
or just use awk:
echo '/aaa/bbb/ccc/ddd' | awk -F/ '{print $NF}'
You can do this :
echo '/aaa/bbb/ccc/ddd' | sed -E 's/(.*\/)(.*)/\2/g' | xargs -n 1 $1
Hope it helps!

using makefile variable in sed command [duplicate]

This question already has an answer here:
Sed command in makefile
(1 answer)
Closed 6 years ago.
I have tried putting the following command in makefile.
#get Local Ip Address
LOCALIP=$(shell ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | awk '{print $1}') &
#get Web Url from User
#read -p "Enter Web Url:" weburl; \
sed -e "\|$LOCALIP $weburl|h; \${x;s|$LOCALIP $weburl||;{g;t};a\\" -e "$LOCALIP $weburl" -e "}" hosts.txt
When I try to execute the command, I expected to get the sed command like following:
sed -e "\|192.168.5.1 www.weburl.com|h; \${x;s|192.168.5.1 www.weburl.com||;{g;t};a\\" -e "192.168.5.1 www.weburl.com" -e "}" hosts.txt
But, I get the following,
sed -e "\|/s/$/OCALIP eburl|h; \" hosts.txt
In Makefiles, variables longer than a single character (i.e. all variables that you're likely to define) needs to be expanded with ${varname}, not $varname. The latter would result in the value of $v concatenated with the string arname, as you discovered.
I won't start to parse the rest of that Makefile as the piping looks a bit questionable.

How to pass output as command line argument in bash? [duplicate]

This question already has answers here:
How to pass command output as multiple arguments to another command
(5 answers)
Closed 3 years ago.
I have this two step bash command:
L=`wc -l testfile | cut -d' ' -f1`
myprogram testfile $L testfile.out
Long story short, myprogram needs the line count as an input.
I want to combine this into one line.
This does not work because using redirect | to - passes stdout stream as a file, not a string.
wc -l testfile | cut -d' ' -f1 | myprogram testfile - testfile.out
Is there a way to combine this into one line?
Use process substitution:
myprogram testfile $(wc -l < testfile) testfile.out
^^^^^^^^^^^^^^^^^^^
This way, wc -l < testfile is evaluated together with the call of the program and you have both commands combined.
Note wc -l < file returns you just the number, so you don't have to do cut or any other thing to clean the output.

Resources