4

I want to pass the output of a command as positional parameter to a script file. I am running the command below:

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

That command will give me a "name". What I want to do is pass that output again to a shell script file as 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
3
  • 2
    A non-zero exit code indicates an error or non-successful condition of some type. Use exit 0 if it is OK. Commented Oct 31, 2017 at 11:58
  • Hi Wai. I notice that, again, this post was rather chatty, featuring more please-halp-me begging and pleading, now edited out by @Inian. I made a note of that style on your prior questions here and here (deleted). As you know, we encourage brevity and technical writing on Stack Overflow. Could I trouble you this time to reply, to acknowledge that you have seen my request? Thank you. Commented Oct 31, 2017 at 22:27
  • 1
    FYI, it would more accurately be [ "$1" = my-domain-name ]. A constant string that has no spaces or wildcard characters doesn't need quotes; a parameter expansion always needs quotes; and == isn't guaranteed to work in [ ] with all shells, whereas = is mandated by the POSIX standard. Commented Oct 31, 2017 at 22:38

3 Answers 3

4

Just define a new function to check the whois output and use the return string in the if condition as below. This way you can avoid the multi-level pipeline while executing the script and rather just control it via a simple function.

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

But if you still want to pass via the command line, do

my-file.sh "$(whois -h 192.168.0.13 google.com | grep -e Domain\ Name)"
Sign up to request clarification or add additional context in comments.

Comments

3

You can use command substitution to do that:

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

It is more straightforward to read than using a pipe and xargs, which is another working solution.

Comments

1

Use awk to select a proper column from the output. Then use xargs to pass its output as an argument to your script.

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

1 Comment

Why two separate tools? awk '/Domain Name/ { print $3 }'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.