Question

I have a question in bash:

Is that a way to check conditions then auto answer questions like what happened in heredoc?

I know two ways that we can auto answer questions(making examples by passwd command):

1) piping

<password> | passwd <username>

2)heredocs

passwd <username> <<EOF
<password>
<password>
EOF

Now, question:

How can put a condition to answer them///

if [ `passwd` -eq 0 ]; then
    <<EOF
     <password>
     <password>
   EOF
fi

for example, this is not possible, any idea?

#!/bin/bash                                                                                                                                                                      

mdadm mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdc1 /dev/sdd1 /dev/sde1 --spare-devices=1 /dev/sdf1
if [ $? -eq 0 ]; then
    <<EOF                                                                                                                                                                        
    ok                                                                                                                                                                           
EOF                                                                                                                                                                              
elif [ $? -ne 0]; then
    <<EOF                                                                                                                                                                        
    failed                                                                                                                                                                       
EOF                                                                                                                                                                              
fi

heredocs and piping or any other solutions are welcome,

I want firstly check conditions after that be able to answer questions

Thanks

Was it helpful?

Solution

With the expect command maybe?

http://www.thegeekstuff.com/2010/12/5-expect-script-command-line-argument-examples/

Here is a little example with FTP since I am not familiar with mdadm. You can see it does something different depending on the response from the server - which is, I think, what you are trying to do.

  # Connect to the FTP server using the "spawn" command.
  spawn ftp $hostname

  # Wait for a login prompt.
  expect -re "(Name|login|Login|Username).*:.*" {

      # Login prompt received. Send username to server.
      exp_send "$username\r"
      exp_send_user "sent username\n"
  } eof {

      # No login prompt received. Display an error.
      exp_send_user "could not connect\n"
  }

OTHER TIPS

I assume you want to check the result of your action. You can do this in two steps:

passwd <user> <<EOF
     <password>
     <password>
EOF

if [ $? -eq 0 ]; then
    echo "Great success"
fi

$? holds the return code of the last executed statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top