Pass command output as a positional parameter to a script file in Linux shell scripts

I want to pass the output of a command as a positional parameter to a script file. Please see my team below.

whois -h 192.168.0.13 google.com | grep -e Domain\ Name

This command will give me a "name". What I want to do is pass this output again to the shell script file as a positional parameter.

my-file.sh:

#!/bin/bash
#My First Script

if [ $1 == "my-domain-name" ]; then
   echo "It is ok"
   exit 1
fi

So basically what I want to do is something like this.

whois -h 192.168.0.13 google.com | grep -e Domain\ Name | -here pass the Name to my-file.sh and run that file
+4
source share
3 answers

whois if, . , script .

get_whois_domainName() {
    whois -h 192.168.0.13 google.com | grep -e Domain\ Name
}

if [ "$(get_whois_domainName)" = "my-domain-name" ]; then
   echo "It is ok"
   exit 0
fi

,

my-file.sh "$(whois -h 192.168.0.13 google.com | grep -e Domain\ Name)"
+4

, :

my-file.sh "$(whois -h 192.168.0.13 google.com | grep -e Domain\ Name)"

, xargs, .

+3

Use awkto select the correct column from the output. Then use xargsto pass your output as an argument to your script.

whois google.com | grep -e Domain\ Name | awk '{ print $3 }' | xargs bash ./test.sh
+1
source

Source: https://habr.com/ru/post/1688452/


All Articles