Score:0

I Have a problems with qoutes in my bash script

au flag

how to do so that in this script which searches for something from the database. he searched for it and changed its name in the terminal. I have a problem with the quotes here. Maybe anyone know how to fix it?

spr-ustawien()
{
echo -e "\e[31;43mSprawdzanie Ustawień\e[0m \e[101mIP $1\e[0m"
intertube=0
while [ $intertube -ne 1 ]; do
ping $1 -c 5
if [ $? -eq 0 ]; then
sshpass -p (pass) ssh -t (user)@$1 "
sudo sqlite3 config.sqlite "SELECT * FROM settings" |grep "SALE_POINT" | awk '{sub(/SALE_POINT/,"\033[1mPunkt Sprzedaży:\033[0m")}1' &&
exit "
echo -e "\e[1;5;32mSprawdzony\e[0m"
intertube=1;
else
echo -e "\e[1;5;31m!!! JEST OFFLINE !!!\e[0m"
break
fi
done
}
Tom Yan avatar
in flag
`"Punkt Sprzedaży"` should be `\"Punkt Sprzedaży\"`?
Black4Killer avatar
au flag
Still doesn't work. awk: line 1: syntax error at or near [ Error: incomplete input
djdomi avatar
za flag
@Black4Killer update the question please with the current state of code `sub(/SALE_POINT/,"\033[1mPunkt` should be if i understand the case `sub(/SALE_POINT/,\"\033[1mPunkt`if you don't want to translate a special character then it must start with \CHAR where char is the special char, you can also debug it when starting the same command with `echo` so it will show you exactly what it's doing meaning `echo sudo sqlite3 config.sqlite "SELECT * FROM settings" |grep "SALE_POINT" | awk '{sub(/SALE_POINT/\,"\033[1mPunkt Sprzedaży:\033[0m\")}1' && exit "`
Black4Killer avatar
au flag
@djdomi they showed me: ```bash: /home/black/.bash_aliases: line 28: syntax error near unexpected token `)' ```
Score:3
ph flag

It looks to me like the basic problem is that quotes don't nest the way you're using them in the ssh command. That is, in this command:

sshpass -p (pass) ssh -t (user)@$1 "
sudo sqlite3 config.sqlite "SELECT * FROM settings" |grep "SALE_POINT" | awk '{sub(/SALE_POINT/,"\033[1mPunkt Sprzedaży:\033[0m")}1' &&
exit "

The " in "SELECT * FROM... is actually a close quote (matching the open quote on the previous line), so rather than SELECT * FROM settings being inside two layers of quotes it's actually completely unquoted (and as a result, the * will expand to a list of files in the local directory, causing who-knows-what chaos).

As djdomi said in a comment, you can see what the arguments to ssh look like after the shell has parsed them (and therefore what'll be sent to the remote system to be executed) by replacing the ssh command with echo. When I run this:

echo "
sudo sqlite3 config.sqlite "SELECT * FROM settings" |grep "SALE_POINT" | awk '{sub(/SALE_POINT/,"\033[1mPunkt Sprzedaży:\033[0m")}1' &&
exit "

it prints:

sudo sqlite3 config.sqlite SELECT file1.txt file2.jpg FROM settings |grep SALE_POINT | awk '{sub(/SALE_POINT/,033[1mPunkt Sprzedaży:033[0m)}1' &&
exit 

The most direct solution is to escape the inner double-quotes, so the local shell will pass them through to the shell on the remote system:

sshpass -p (pass) ssh -t (user)@$1 "
sudo sqlite3 config.sqlite \"SELECT * FROM settings\" |grep \"SALE_POINT\" | awk '{sub(/SALE_POINT/,\"\033[1mPunkt Sprzedaży:\033[0m\")}1' &&
exit "

Note tht you must escape all of them, even the ones inside the single-quoted awk command, because those single-quotes don't mean anything to the local shell.

... but I'd recommend simplifying this a bit. I don't see any reason that grep and awk need to be run on the remote system rather than the local system (and the exit command isn't doing anything useful, since it's going to exit anyway). So you could move the grep and awk commands outside the ssh command:

sshpass -p (pass) ssh -t (user)@$1 "sudo sqlite3 config.sqlite \"SELECT * FROM settings\"" |
    grep "SALE_POINT" |
    awk '{sub(/SALE_POINT/,"\033[1mPunkt Sprzedaży:\033[0m")}1'

And I always hate to see grep used before awk, when awk is perfectly capable of doing everything itself:

sshpass -p (pass) ssh -t (user)@$1 "sudo sqlite3 config.sqlite \"SELECT * FROM settings\"" |
    awk '/SALE_POINT/ {sub(/SALE_POINT/,"\033[1mPunkt Sprzedaży:\033[0m"); print}'

I'd also recommend double-quoting all variable references (e.g. ping "$1" -c 5 instead of just ping $1 -c 5), and replacing this:

ping $1 -c 5
if [ $? -eq 0 ]; then

with just:

if ping "$1" -c 5; then

And finally I'd recommend printf instead of echo -e -- it's way more predictable. printf is a bit more complex to use -- the first argument is a format string that tells it how to print any remaining arguments, and it doesn't automatically add a newline at the end (so add one explicitly with \n) -- but it's less likely to break because of some change in the shell's version of echo (as happened to me a while back...). So use e.g.

printf "\e[31;43m%s\e[0m \e[101m%s1\e[0m\n" "Sprawdzanie Ustawień" "IP $1"

Oh, and I always recommend running your scripts through shellcheck.net -- it'll point out many common mistakes and bad practices.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.