Script exits on line calling tput when invoked from SecureCRT
Clash Royale CLAN TAG#URR8PPP
Script exits on line calling tput when invoked from SecureCRT
I am using locale en_US.UTF8 When using the bash -v script_name.sh it gets to a variable that has single quotes - example below. Have tried manually setting UTF8 on securecrt session, validated all locale settings are en_US.
When it errors out there is no error message, just the last variable value and then back to terminal prompt
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8
CODE EXAMPLE
#!/bin/bash
....some text eliminated ....
set -e
########## Variable ##########
SUDO=""
BASH_C="bash -c"
SLEEP_SEC=10
LOG_OUTPUT="/tmp/$0##*/$(date +%Y-%m-%d.%H-%M)"
CURRENT_USER="$(id -un 2>/dev/null || true)"
INSTALLATION_DIR="/srv/app/ilp-connector"
CONNECTOR_REPO="https://github.com/interledgerjs/ilp-connector.git"
########## Nodejs ##########
NODEJS_RPM_URL="https://rpm.nodesource.com/setup_10.x"
NODEJS_DEB_URL="https://deb.nodesource.com/setup_10.x"
########## Certbot ##########
CERTBOT_AUTO_URL="https://dl.eff.org/certbot-auto"
########## Constant ##########
SUPPORT_DISTRO=(debian ubuntu fedora centos)
#Color Constant
RED="tput setaf 1"
SINGLE QUOTES HERE ->>>> GREEN=`tput setaf 2`
ERROR SECTION ->>>> tput setaf 2
GREEN
tput setaf 2
If the backquotes bother you, in bash you can use
$()
, e.g. $(tput setaf 2)
. Actually it’s even better because you can nest an expression inside another, unlike backquotes.– vdavid
Aug 12 at 10:08
$()
$(tput setaf 2)
True, they are backticks. That was my oversight. Not my script. When the scriot is run and reaches this variable RED and it has backticks it exits prematurely. I changed them to double quotes and it moves past that point and fails on the next line. The real question is why is bash failing when trying to assign a command to variable using backticks?
– jdimstrnate
Aug 12 at 17:14
I will try using the $() and see if that fixes the issue. I understand this is a deprecated style so it may not be compatible?
– jdimstrnate
Aug 12 at 17:36
I have tried the $(). The following is the output from that section. Returns to temerinal immediately when there is more in the script so it basically errors out without an error message. [root@ilp01 useruser]# bash -v ilp-install.sh #!/bin/bash --utf8 .........eliminated some code for brevity........ SUPPORT_DISTRO=(debian ubuntu fedora centos) #Color Constant RED=$(tput setaf 1) [root@ilp01 useruser]#
– jdimstrnate
Aug 12 at 20:22
2 Answers
2
The solution albeit partial, is that the SecureCRT session or shell session in some way was not parsing the script properly. As soon as I used the VMware console and opened up a terminal it started to work.... So Ill just use that and drop the issue at this point. Somewhat frustrating but it now I know.
figured it out. It was secureCRT default terminal options. I changed it to XTERM and Linux. Both work. Closing discussion.
– jdimstrnate
Aug 13 at 4:57
Change:
GREEN=`tput setaf 2`
...to...
GREEN=$(tput setaf 2) ||:
:
is a synonym for true
. This makes a failure from tput
be ignored, so your script continues to execute (even with set -e
) even if the color code for green can't be detected.
:
true
tput
set -e
That said, I strongly advise against using set -e
; see BashFAQ #105 for an in-depth discussion.
set -e
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I'm not clear on what's going wrong here. Also, those are backquotes (aka backticks), not regular single-quotes, and they do something very different -- they execute their contents as a command, then use its output as the value at that point in the command line. So rather than setting the variable
GREEN
to the string "tput setaf 2", it sets it to the output of the commandtput setaf 2
. Is that not what you intended?– Gordon Davisson
Aug 12 at 4:49