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.