Writing a shell script to validate the output value of a command executed.
[en2@localhost ~]$ echo $actualCount 31
[en@localhost ~]$ $actualCount -bash: $'31rr': command not found
Value contains rr. Variable trying to variable against the output value.
[eden0292@localhost ~]$ echo $IMPORT_DOCUMENT_COUNT 31
When i am trying to check the condition after removing it works. Same executed in the shell script is not working.
`[en@localhost ~]$ ORIGINAL=$(echo ${actualCount} | tr -d ‘r’)
[en@localhost ~]$ if [ “${ORIGINAL}” = “${IMPORT_DOCUMENT_COUNT}” ]; then echo SUCCESS; else echo FAILED; fi
SUCCESS
[enlocalhost ~]$ AFTER=echo $actualCount | sed 's/\r//g'
[en@localhost ~]$ if [ “${AFTER}” = “${IMPORT_DOCUMENT_COUNT}” ]; then echo success; else echo failed; fi
success`
**Shell script:
**
`#!/bin/bash
actualCount=”$(kubectl –namespace=”${K8S_NAMESPACE}” exec -it “${pod_id}” -c “${MONGO_CONTAINER_NAME}” — bash -c ‘export NODE_DISABLE_COLORS=1; mongo -u ‘”${USER_NAME}”‘ -p ‘”${USER_PASSWD}”‘ –host ‘”${primary_host//[$’trn ‘]}”‘ –tlsCAFile /data/db/private/cacert.pem –tlsCertificateKeyFile /data/db/private/client.pem –tls –authenticationDatabase ‘”${DB_NAME}”‘ –eval “db.getSiblingDB(“‘”${DB_NAME}”‘”).getCollection(“‘”${COLLECTION_NAME}”‘”).countDocuments({})”‘ | tail -1 )”
ORIGINAL=$(echo ${actualCount} | tr -d ‘r’)
if [ “${ORIGINAL}” = “${IMPORT_DOCUMENT_COUNT}” ]; then echo SUCCESS; else echo FAILED; fi
AFTER=echo $actualCount | sed 's/\r//g'
if [ “${AFTER}” = “${IMPORT_DOCUMENT_COUNT}” ]; then echo sucess; else echo failed; fi`
Output:
Unable to use a TTY – input is not a terminal or the right kind of file
FAILED
failed
Gopi P is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.