0

I need to pass input from my bash script to the commands I run inside the script. I have no idea how to do it. I've searched all day for something like this in bash.

#!/bin/env bash

# This script is used for generating ssl cert's for websites
# ==========================================================
# Version 1.0
# ==========================================================

# Working dir
# ===========
cd "$(dirname "$0")"

# Debugging
# =========
set -x

# Imput options
# =============
read -p "Domain name: " domain_name;
read -p "Enter password: " pass;


# Verify if there is imput "conditional expressions"
# man test; help [[
# ==================================================
if [[ $domain_name ]]; then
  openssl genrsa -aes256 -out root/ca/intermediate/private/${domain_name}.key.pem 2048
  chmod 400 root/ca/intermediate/private/${domain_name}.key.pem
  openssl req -config root/ca/intermediate/openssl.cnf -key root/ca/intermediate/private/${domain_name}.key.pem -new -sha256 -out root/ca/intermediate/csr/${domain_name}.csr.pem
  openssl ca -config root/ca/intermediate/openssl.cnf -extensions server_cert -days 475 -notext -md sha256 -in root/ca/intermediate/csr/${domain_name}.csr.pem -out root/ca/intermediate/certs/${domain_name}.cert.pem
  chmod 444 root/ca/intermediate/certs/${domain_name}.cert.pem
else
  echo "Insert a domain name."
fi
if openssl x509 -noout -text -in intermediate/certs/${domain_name}.cert.pem; then
  openssl verify -CAfile root/ca/intermediate/certs/ca-chain.cert.pem intermediate/certs/${domain_name}.cert.pem
fi

Practically i need the script not fail and automate the creation of ssl self signed certificates.

The input that i need to pass is: password, domain name.

  • What is the error that are you getting? – Kira Dec 15 '15 at 16:10
1

You can do that in bash with something like that:

#!/bin/bash
domain="domain.com"
pass="somethingCompleX"
your_script.sh <<EOF
$domain
$password
EOF

with 'your_script.sh', the script given in your question.

This syntax permits to pass some strings in input of a script. The first <<EOF gives the 'End Of File' tag that represent the end of the strings you want to pass to your script. All the characters after will be passed to your script, so the

your_script.sh <<EOF
foo
bar
EOF

equals to

$ your_script.sh
Domain name: foo
password: bar

you can also refer to this answer on stackoverflow

  • Thanks for the answer but i don't kind of understand it. – EmbargoLacuna Dec 15 '15 at 16:21
  • I edited my answer, is it more understandable for you ? – Drag Dec 15 '15 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.