zsh shell problem specifically when I am trying to get output - shell

read -p "Enter your name: " NAME
echo "Hello $NAME, nice to meet you".```
In terminal:
ks#USER Desktop % ./script.sh
./script.sh:read:18: -p: no coprocess
Hello , nice to meet you.
[picture of the problem ][1]
[1]: https://i.stack.imgur.com/ZpVri.png

In ZSH -p doesn't mean prompt as in Bash. See man zshbuiltins:
read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
(...)
-p Input is read from the coprocess.
To get a prompt with ZSH implementation of read:
$ read "?Enter your name: " NAME
Enter your name: myname
$ echo $NAME
myname

Related

error in executing interactive bash script to run docker containers

Hello guys i am new in shell scripting and I tried run the script below but have following error message:
#!/bin/bash
echo "Enter the name of the image whose container you want to run: "
read container
echo "Enter a name for this container: "
read name
echo "do you want to run in detatch mode? "
read d
if [ $d -eq yes ]
then
docker run --name $name -P -d $container
elif [ $d -eq no ]
then
docker run --name $name -P $container
else
echo "invalid input"
fi
This produces the following error messages:
./main.sh: line 9: [: yes: integer expression expected
./main.sh: line 12: [: yes: integer expression expected
-eq compares two integers
use = in order to compare two strings
for more info please refer to: https://stackoverflow.com/a/20449556/9881735
for example:
[ $d = "yes" ]

bash script: how to "exit" from sourced script, and allow to work non sourced?

I have a script that I'd like people to source, but optionally so. So they can run it with or without sourcing it, it's up to them.
e.g. The following should both work:
$ . test.sh
$ test.sh
The problem is, test.sh contains exit statements if correct args aren't passed in. If someone sources the script, then the exit commands exit the terminal!
I've done a bit of research and see from this StackOverflow post that I could detect if it's being sourced, and do something different, but what would that something different be?
The normal way to exit from a sourced script is simply to return (optionally adding the desired exit code) outside of any function. Assuming that when run as a command we have the -e flag set, this will also exit from a shell program:
#!/bin/sh -eu
if [ $# = 0 ]
then
echo "Usage $0 <argument>" >&2
return 1
fi
If we're running without -e, we might be able to return || exit instead.
There may be better ways to do this, but here's a sample script showing how I got this to work:
bparks#home
$ set | grep TESTVAR
bparks#home
$ ./test.sh
Outputs some useful information to the console. Please pass one arg.
bparks#home
$ set | grep TESTVAR
bparks#home
$ . ./test.sh
Outputs some useful information to the console. Please pass one arg.
bparks#home
$ set | grep TESTVAR
bparks#home
$ ./test.sh asdf
export TESTVAR=me
bparks#home
$ set | grep TESTVAR
bparks#home
$ . ./test.sh asdf
bparks#home
$ set | grep TESTVAR
TESTVAR=me
bparks#home
$
test.sh
#!/usr/bin/env bash
# store if we're sourced or not in a variable
(return 0 2>/dev/null) && SOURCED=1 || SOURCED=0
exitIfNotSourced(){
[[ "$SOURCED" != "0" ]] || exit;
}
showHelp(){
IT=$(cat <<EOF
Outputs some useful information to the console. Please pass one arg.
EOF
)
echo "$IT"
}
# Show help if no args supplied - works if sourced or not sourced
if [ -z "$1" ]
then
showHelp
exitIfNotSourced;
return;
fi
# your main script follows
# this sample shows exporting a variable if sourced,
# and outputting this to stdout if not sourced
if [ "$SOURCED" == "1" ]
then
export TESTVAR=me
else
echo "export TESTVAR=me"
fi
Checkout this answer for better description and porper solution.
And here is how it is used in docker-entrypoint.sh in official Mysql image:
# check to see if this file is being run or sourced from another script
_is_sourced() {
# https://unix.stackexchange.com/a/215279
[ "${#FUNCNAME[#]}" -ge 2 ] \
&& [ "${FUNCNAME[0]}" = '_is_sourced' ] \
&& [ "${FUNCNAME[1]}" = 'source' ]
}

sqlcmd in bash file - store to variable

Running in a docker container, I am trying to determine if a table exits in the mssql database:
RESULT='/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -d master -i "SELECT name FROM master.sys.databases WHERE name = N'MyTable'"'
if [ "$RESULT" == "MyTable" ]; then
echo YES
fi
echo "$RESULT"
echo "$RESULT" always just outputs the entire command as a string, its not getting executed. So its just assigning it as a sting...
How can I execute it and assign the result?
Bash does not execute commands defined in single quotes. Single quotes are used for string definitions.
What you try to do is called command substitution. And it can be done in two different ways:
RESULT=`command`
RESULT=$(command)
So your example should look like this:
RESULT=`/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P $SA_PASSWORD -d master -i "SELECT name FROM master.sys.databases WHERE name = 'MyTable'`
if [ "$RESULT" == "MyTable" ]; then
echo YES
fi
echo "$RESULT"
You can find more information regarding command substitution here:
https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html

Chrome OS bash script if statement error

Im running Chrome OS in dev mode, and I am creating a bash file. The code looks like so:
echo -n "Username: "
read username: "
if [ $username == "mycroft" ]; then
echo "Correct!"
fi
My problem is that when I run the code in crosh with /bin/sh ./login.sh, after I type
in mycroft, I get this error:
[: 4: mycroft: unexpected operator
What did I do wrong?
Your problem is on the second line
echo -n "Username: "
read username
if [ $username = "mycroft" ]; then
echo "Correct!"
fi

bash configuration templating

I am using a bash script to install and configuration an application. I am trying to use a template where I can fill in values from variables and then save it to its proper location. This works fine with just variables, but I am unable to use any conditionals. Are there any better ways of doing this?
Heres my example template:
#!/bin/bash
cat <<EOF
DEFAULTS: &DEFAULTS
adapter: $DB
host: $DB_HOST
username: $DB_USER
password: $DB_PASS
database: $DB_DATABASE
if [ ! $DB_RECONNECT = ""]; then
reconnect: $DB_RECONNECT
fi
if [ ! $DB_TIMEOUT = ""]; then
timeout: $DB_TIMEOUT
fi
EOF
And then I use source template.sh > /path/to/file to evaluate and save the file.
You don't have to enclose everything in the heredoc
cat <<EOF
...
database: $DB_DATABASE
EOF
if [ -z "$DB_RECONNECT" ]; then
echo "reconnect: $DB_RECONNECT"
fi
if [ -z "$DB_TIMEOUT" ]; then
echo "timeout: $DB_TIMEOUT"
fi
You can use the command tpage like in this simple example :
$ cat /tmp/l.tpl
DEFAULTS: [%def%]
adapter[%db%]
$ tpage --define def=foo --define db=bar --interpolate /tmp/l.tpl
DEFAULTS: foo
adapterbar
tpage is a command coming with the well known Perl module Template::Toolkit, but no need to know Perl to use it. You can do some conditional as well, see conditional
Project : http://www.template-toolkit.org/

Resources