Score:2

bash script error in created command line

id flag

I'm trying to make a script which "builds" a configure command line and then runs it, but it fails with an error. I'm very new to bash script (i'm a Windows user) and i don't know what i'm doing wrong.

This is my script:

            #! /bin/sh
        # https://www.guru99.com/introduction-to-shell-scripting.html

        export PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf/pkgconfig

        PARAMS=""
        PARAMS="$PARAMS --disable-shared"
        PARAMS="$PARAMS --disable-debug"
        PARAMS="$PARAMS --with-gtk=2"
        PARAMS="$PARAMS --with-libtiff=no"
        PARAMS="$PARAMS --with-opengl=no"
        PARAMS="$PARAMS --enable-unicode"
        PARAMS="$PARAMS --with-libpng=builtin"
        PARAMS="$PARAMS --with-libjpeg=builtin"
        PARAMS="$PARAMS --enable-std_string_conv_in_wxstring"
        PARAMS="$PARAMS --host=arm-linux-gnueabihf"
        PARAMS="$PARAMS --build=x86_64-linux-gnu"

        GTK_LIBS=$(arm-linux-gnueabihf-pkg-config --libs gtk+-2.0)
        echo "GTK_LIBS = " $GTK_LIBS
        echo
        PARAMS="$PARAMS GTK_LIBS=\"$GTK_LIBS\""

        GTK_CFLAGS=$(arm-linux-gnueabihf-pkg-config --cflags gtk+-2.0)
        echo "GTK_CFLAGS = " $GTK_CFLAGS
        echo

        COMMAND="../configure $PARAMS GTK_CFLAGS=\"$GTK_CFLAGS\""
        echo "COMMAND = " $COMMAND
        echo

        echo "Running COMMAND"
        $COMMAND

This is the output:

GTK_LIBS = -L/usr/lib/arm-linux-gnueabihf -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lharfbuzz -lfontconfig -lfreetype

GTK_CFLAGS = -pthread -I/usr/include/gtk-2.0 -I/usr/lib/arm-linux-gnueabihf/gtk-2.0/include -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/arm-linux-gnueabihf/glib-2.0/include -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/libpng16

COMMAND = ../configure --disable-shared --disable-debug --with-gtk=2 --with-libtiff=no --with-opengl=no --enable-unicode --with-libpng=builtin --with-libjpeg=builtin --enable-std_string_conv_in_wxstring --host=arm-linux-gnueabihf --build=x86_64-linux-gnu GTK_LIBS="-L/usr/lib/arm-linux-gnueabihf -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lharfbuzz -lfontconfig -lfreetype" GTK_CFLAGS="-pthread -I/usr/include/gtk-2.0 -I/usr/lib/arm-linux-gnueabihf/gtk-2.0/include -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/arm-linux-gnueabihf/glib-2.0/include -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/libpng16"

Running COMMAND

configure: error: unrecognized option: `-lgtk-x11-2.0'

Try `../configure --help' for more information

I don't understand why there's the error: it seems like i didn't add the doublequotes, but they are there and if i just copy/paste the COMMAND from the output in the prompt it works.

I tried searching for answers here... which managed me to add the escaped quotes in the script, but that's all....

What am i missing?

Score:0
vn flag

How about exporting the variables before running the command, instead of them being part of the command?

Try this:

#!/bin/sh
# https://www.guru99.com/introduction-to-shell-scripting.html

export PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf/pkgconfig

PARAMS=""
PARAMS="$PARAMS --disable-shared"
PARAMS="$PARAMS --disable-debug"
PARAMS="$PARAMS --with-gtk=2"
PARAMS="$PARAMS --with-libtiff=no"
PARAMS="$PARAMS --with-opengl=no"
PARAMS="$PARAMS --enable-unicode"
PARAMS="$PARAMS --with-libpng=builtin"
PARAMS="$PARAMS --with-libjpeg=builtin"
PARAMS="$PARAMS --enable-std_string_conv_in_wxstring"
PARAMS="$PARAMS --host=arm-linux-gnueabihf"
PARAMS="$PARAMS --build=x86_64-linux-gnu"

GTK_LIBS=$(arm-linux-gnueabihf-pkg-config --libs gtk+-2.0)
echo "GTK_LIBS = " $GTK_LIBS
echo

GTK_CFLAGS=$(arm-linux-gnueabihf-pkg-config --cflags gtk+-2.0)
echo "GTK_CFLAGS = " $GTK_CFLAGS
echo

COMMAND="../configure $PARAMS"
echo "COMMAND = " $COMMAND
echo

echo "Setting variables and running COMMAND"
export GTK_LIBS
export GTK_CFLAGS
$COMMAND
Score:0
hr flag

First, unless you have modified the default Ubuntu /bin/sh, it will be executed as a dash script rather than a bash script.

The issue is that you can't convert a whitespace-containing string into a properly shell-quoted list of tokens by using literal quotes and then leaving the resulting expansion unquoted. Simplifying your code, you're essentially doing:

PARAMS=foo
GTK_LIBS="bar baz"
PARAMS="$PARAMS GTK_LIBS=\"$GTK_LIBS\""

and then expanding $PARAMS without quoting, which (in both bash dash) will result in word splitting into the following literal tokens:

$ printf '%s\n' $PARAMS
foo
GTK_LIBS="bar
baz"

If you actually change the shell to bash then you can get around this using arrays:

#!/bin/bash

PARAMS=("foo")
GTK_LIBS=("bar" "baz")
PARAMS+=(GTK_LIBS="${GTK_LIBS[@]}")

then expand the array as "${PARAMS[@]}", which correctly tokenizes the arguments:

printf '%s\n' "${PARAMS[@]}"
foo
GTK_LIBS=bar baz

See also:

I sit in a Tesla and translated this thread with Ai:

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.