LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   "let" command error in bash script (https://www.linuxquestions.org/questions/programming-9/let-command-error-in-bash-script-4175652407/)

AnneRanch 04-18-2019 09:23 PM

"let" command error in bash script
 
I have found this simple code to echo options passed to a function.
I does work , however, I am sometime getting strange unwanted output.
At present it is harmless, but I like to know what is the cause..

PS Since the "cnt" is really not "initialized", Could that be a problem?

Test code

Code:

# Printing the data available in $@
echo "Values of \"\$@\":"
for arg in "$@"
do
  echo "Arg #$cnt= $arg"
  let "cnt+=1"
done

Correct output

Quote:

Values of "$@":
Arg #= --exists
Arg #= --print-errors
Arg #= glib-2.0 >= 2.28
@line exec pkg-config trace TOK (?)


Incorrect output

Quote:

Values of "$@":
Arg #= --atleast-pkgconfig-version
/usr/bin/arm-linux-gnueabihf-pkg-config: 104: /usr/bin/arm-linux-gnueabihf-pkg-config: let: not found
Arg #= 0.9.0
/usr/bin/arm-linux-gnueabihf-pkg-config: 104: /usr/bin/arm-linux-gnueabihf-pkg-config: let: not found
@line exec pkg-config trace TOK (?)



Values of "$@":
Arg #= --short-errors
/usr/bin/arm-linux-gnueabihf-pkg-config: 104: /usr/bin/arm-linux-gnueabihf-pkg-config: let: not found
Arg #= --print-errors
/usr/bin/arm-linux-gnueabihf-pkg-config: 104: /usr/bin/arm-linux-gnueabihf-pkg-config: let: not found
Arg #= --cflags
/usr/bin/arm-linux-gnueabihf-pkg-config: 104: /usr/bin/arm-linux-gnueabihf-pkg-config: let: not found
Arg #= --libs
/usr/bin/arm-linux-gnueabihf-pkg-config: 104: /usr/bin/arm-linux-gnueabihf-pkg-config: let: not found
Arg #= glib-2.0 >= 2.28
/usr/bin/arm-linux-gnueabihf-pkg-config: 104: /usr/bin/arm-linux-gnueabihf-pkg-config: let: not found

I have copied the code to the beginning of the file executed from the main script with same result. I was trying to see if the file modifies the option parameters passed to it, apparently it does not.
Note that the "error #" is indicating problem is indeed associated with "let" command.



Quote:

@line entry print options passed (?)
Values of "$@":
Arg #= --atleast-pkgconfig-version
/usr/bin/arm-linux-gnueabihf-pkg-config: 16: /usr/bin/arm-linux-gnueabihf-pkg-config: let: not found
Arg #= 0.9.0
/usr/bin/arm-linux-gnueabihf-pkg-config: 16: /usr/bin/arm-linux-gnueabihf-pkg-config: let: not found
@line entry print options passed (?)



astrogeek 04-18-2019 10:16 PM

That probably means that you are not running that as bash, but in some other shell environment.

Check the interpreter line, is it #!/bin/bash or #!/bin/sh?

You also say it is called from another script so it is hard to know what the actual running context is from information provided.

Fix it by changing that line to not use let, perhaps:

Code:

cnt=$((cnt+1))

or simply

((cnt++))


AnneRanch 04-19-2019 01:06 PM

I did changed "let " to ((cnt++)) with same results.
I think you are correct - the problem is when the "side" process returns to the main script and it generates "no".
That must do something to the positional parameters at that point.



Quote:

@line CCC DEBUG ENTRY # /usr/bin/arm-linux-gnueabihf-pkg-config pkg-config wrapper for cross-building

@line Sets pkg-config search path to search multiarch and historical cross-compiling paths. TOK echo

@line test PKG_CONFIG_LIBDIR = empty here TOK (?)
@line entry print options passed (?)
Values of "$@":
Arg #= --exists
Arg #= --print-errors
Arg #= glib-2.0 >= 2.28
@line entry print options passed (?)
@line test PKG_CONFIG_LIBDIR = valid how (? ) TOK Here (?)
@line test triplet PKG_CONFIG_LIBDIR = /usr/local/arm-linux-gnueabihf/lib/pkgconfig now set here TOK (?)
@line test share PKG_CONFIG_LIBDIR = /usr/local/arm-linux-gnueabihf/lib/pkgconfig:/usr/local/share/pkgconfig now set here TOK (?)
@line test share PKG_CONFIG_LIBDIR = /usr/local/lib/arm-linux-gnueabihf/pkgconfig:/usr/local/arm-linux-gnueabihf/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/arm-linux-gnueabihf/lib/pkgconfig more set (?) TOK (?)
@line test share PKG_CONFIG_LIBDIR = /usr/local/lib/arm-linux-gnueabihf/pkgconfig:/usr/local/arm-linux-gnueabihf/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/arm-linux-gnueabihf/lib/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig last set (?) TOK (?)
@line export PKG_CONFIG_LIBDIR = /usr/local/lib/arm-linux-gnueabihf/pkgconfig:/usr/local/arm-linux-gnueabihf/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/arm-linux-gnueabihf/lib/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig (?)
Values of "$@":
Arg #= --exists
Arg #= --print-errors
Arg #= glib-2.0 >= 2.28
@line exec pkg-config trace TOK (?)

THIS MUST BE THE PROBLEM
no

@line CCC DEBUG ENTRY # /usr/bin/arm-linux-gnueabihf-pkg-config pkg-config wrapper for cross-building

@line Sets pkg-config search path to search multiarch and historical cross-compiling paths. TOK echo

@line test PKG_CONFIG_LIBDIR = empty here TOK (?)
@line entry print options passed (?)
Values of "$@":
Arg #= --atleast-pkgconfig-version
/usr/bin/arm-linux-gnueabihf-pkg-config: 16: /usr/bin/arm-linux-gnueabihf-pkg-config: cnt++: not found
Arg #= 0.20
/usr/bin/arm-linux-gnueabihf-pkg-config: 16: /usr/bin/arm-linux-gnueabihf-pkg-config: cnt++: not found
@line entry print options passed (?)
@line test PKG_CONFIG_LIBDIR = valid how (? ) TOK Here (?)
@line test triplet PKG_CONFIG_LIBDIR = /usr/local/arm-linux-gnueabihf/lib/pkgconfig now set here TOK (?)
@line test share PKG_CONFIG_LIBDIR = /usr/local/arm-linux-gnueabihf/lib/pkgconfig:/usr/local/share/pkgconfig now set here TOK (?)
@line test share PKG_CONFIG_LIBDIR = /usr/local/lib/arm-linux-gnueabihf/pkgconfig:/usr/local/arm-linux-gnueabihf/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/arm-linux-gnueabihf/lib/pkgconfig more set (?) TOK (?)
@line test share PKG_CONFIG_LIBDIR = /usr/local/lib/arm-linux-gnueabihf/pkgconfig:/usr/local/arm-linux-gnueabihf/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/arm-linux-gnueabihf/lib/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig last set (?) TOK (?)
@line export PKG_CONFIG_LIBDIR = /usr/local/lib/arm-linux-gnueabihf/pkgconfig:/usr/local/arm-linux-gnueabihf/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/arm-linux-gnueabihf/pkgconfig:/usr/arm-linux-gnueabihf/lib/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig (?)
Values of "$@":
Arg #= --atleast-pkgconfig-version
/usr/bin/arm-linux-gnueabihf-pkg-config: 119: /usr/bin/arm-linux-gnueabihf-pkg-config: cnt++: not found
Arg #= 0.20
/usr/bin/arm-linux-gnueabihf-pkg-config: 119: /usr/bin/arm-linux-gnueabihf-pkg-config: cnt++: not found
@line exec pkg-config trace TOK (?)
configure_419: error: GLib >= 2.28 is required



I am not sure how arm-linux-gnueabihf-pkg-config was created , but it is

#! /bin/sh

same as the "main" script.

Anyway, appreciate your input, but this is no big deal, just was curious why is this happening.
I need to clean-up the main problems and then I will have time to get back to this minor one.

Cheers

ondoho 04-20-2019 01:58 AM

Code:

ls -al $SHELL
help let


AnneRanch 04-20-2019 09:00 AM

Quote:

Originally Posted by ondoho (Post 5986868)
Code:

ls -al $SHELL
help let


Nice, but how does it explain why the code works OK in one case and then it does not work in another?

BW-userx 04-20-2019 09:56 AM

that is your loop code, all of it?

cnt not initialized

Code:

#!/bin/bash


ct1 ()
{

# Printing the data available in $@
echo "Values of \"\$@\":"
for arg in "$@"
do
  echo "Arg #$cnt= $arg"
  let "cnt+=1"
done
}
 
ct1 $@


echo "NEXT"
echo

ct2()
{
# Printing the data available in $@
echo "Values of \"\$@\":"
for arg in "$@"
do
  echo "Arg #$cnt= $arg"
 ((cnt++))
done
}
ct2 $@

results
Code:

$ ./letters /pack/gp/de -exist -hne
Values of "$@":
Arg #= /pack/gp/de
Arg #1= -exist
Arg #2= -hne
NEXT

Values of "$@":
Arg #3= /pack/gp/de
Arg #4= -exist
Arg #5= -hne

it works for me. something you're not showing is missing.

pan64 04-20-2019 10:15 AM

Quote:

Originally Posted by AnneRanch (Post 5986967)
Nice, but how does it explain why the code works OK in one case and then it does not work in another?

probably you don't understand, but if you won't post the result we cannot explain.

This is definitely a problem of mixing shells. Your code is running fine with bash, but will not run with sh.
You can simply try it by creating a small script (containing the loop from your original post):
Code:

user@host:/tmp$ bash ./a.sh d f g
Values of "$@":
Arg #= d
Arg #1= f
Arg #2= g
user@host:/tmp$ sh ./a.sh d f g
Values of "$@":
Arg #= d
./a.sh: 6: ./a.sh: let: not found
Arg #= f
./a.sh: 6: ./a.sh: let: not found
Arg #= g
./a.sh: 6: ./a.sh: let: not found
user@host:/tmp$ cat a.sh
# Printing the data available in $@
echo "Values of \"\$@\":"
for arg in "$@"
do
  echo "Arg #$cnt= $arg"
  let "cnt+=1"
done

here there was no shebang.
But you can also try to insert #!/bin/bash and #!/bin/sh, make it executable and run by ./a.sh and you will see the same result.

AnneRanch 04-20-2019 11:22 AM

Quote:

Originally Posted by pan64 (Post 5986980)
probably you don't understand, but if you won't post the result we cannot explain.

This is definitely a problem of mixing shells. Your code is running fine with bash, but will not run with sh.
You can simply try it by creating a small script (containing the loop from your original post):
Code:

user@host:/tmp$ bash ./a.sh d f g
Values of "$@":
Arg #= d
Arg #1= f
Arg #2= g
user@host:/tmp$ sh ./a.sh d f g
Values of "$@":
Arg #= d
./a.sh: 6: ./a.sh: let: not found
Arg #= f
./a.sh: 6: ./a.sh: let: not found
Arg #= g
./a.sh: 6: ./a.sh: let: not found
user@host:/tmp$ cat a.sh
# Printing the data available in $@
echo "Values of \"\$@\":"
for arg in "$@"
do
  echo "Arg #$cnt= $arg"
  let "cnt+=1"
done

here there was no shebang.
But you can also try to insert #!/bin/bash and #!/bin/sh, make it executable and run by ./a.sh and you will see the same result.

It is no necessary to tell me that I do not understand.
Using same attitude - it would help if you read the post. See attached.

Either way - so far nobody seems to be able to answer or guess why it does not work PART of the time.
It would be helpful to post replies actually answering the original question without getting sidetracked with references to man or doc.

The standard positional parameters are available / passed to the code and the correct prints reflects that,
regardless if it posts the "not found" error or not.

What else do I need to supply to analyse the issue?



Quote:


probably you don't understand, but if you won't post the result ??? we cannot explain.



I am not sure how arm-linux-gnueabihf-pkg-config was created , but it is

#! /bin/sh

same as the "main" script.


BW-userx 04-20-2019 11:30 AM

what is the script, not just that little bit, because me and another ran your little sniplet and they both worked. therefore it has to be something you are not showing up in order for anyone else to try and deduce what the actual problem is.

pan64 04-20-2019 11:48 AM

Quote:

Originally Posted by AnneRanch (Post 5986991)
Either way - so far nobody seems to be able to answer or guess why it does not work PART of the time.

When it is executed using bash it works, when it is executed using sh it won't.

You can ask why is it executed with bash and/or sh. The answer: depends on how it was invoked.

AnneRanch 04-20-2019 08:16 PM

SOLVED
 
Code:

for i
do
 echo "Argument  =  $i "
done

As far as I can tell the original, inherited code failed to use the positional parameters correctly.
Current version does not print the numbers of the parameters, so did not the original code.
The numbers are irrelevant, I need just to verify the "arguments" AKA options passed to the function.

Thanks for all the help provided by the group.

MadeInGermany 04-21-2019 02:51 AM

Still unclear.
Code:

for i
is identical with
Code:

for i in "$@"
Maybe your not shown code manipulated the arg variable? Then using another variable solves it.

pan64 04-21-2019 02:58 AM

just a comment: I have never heard something like this: "code failed to use the positional parameters correctly". Probably you mean incorrect handling/implementation. The positional parameters work perfectly in all the apps/languages I tried.

ondoho 04-21-2019 04:14 AM

still didn't show us the script, prefers to be opinionated instead.

AnneRanch 04-21-2019 09:11 AM

Quote:

Originally Posted by ondoho (Post 5987144)
still didn't show us the script, prefers to be opinionated instead.


If it helps YOU , here it is.
Have a ball, but since the problem is solved keep in mind I am NOT interested in your analysis of this script and no longer subscribe to this thread.


Code:

#! /bin/sh 
#CCC define debug
 
: '  this does not work here
if test -n  "${enable_debug+set}" = set; then :
printf "\033[1;32m@line $LINENO DEBUG test --enable-debug is  set in  arm-linux-gnueabihf-pkg-config    (?)  \033[0m\n"
else
printf "\033[1;31m@line $LINENO DEBUG test --enable-debug is NOT  in  arm-linux-gnueabihf-pkg-config    (?)  \033[0m\n"
fi
'




#DEBUG = yes
#if [ $DEBUG = set ]
#then
echo "\n@line $LINENO CCC DEBUG ENTRY  # /usr/bin/arm-linux-gnueabihf-pkg-config pkg-config wrapper for cross-building"
#fi
echo "@line $LINENO Sets pkg-config search path to search multiarch and historical cross-compiling paths.  TOK echo "
echo "@line $LINENO test PKG_CONFIG_LIBDIR = $PKG_CONFIG_LIBDIR empty here  TOK  (?) "




# Sets pkg-config search path to search multiarch and historical cross-compiling paths.
# If the user has already set PKG_CONFIG_LIBDIR, believe it (even if empty):
# it's documented to be an override
if [ x"${PKG_CONFIG_LIBDIR+set}" = x ];
then
echo "@line $LINENO test PKG_CONFIG_LIBDIR = $PKG_CONFIG_LIBDIR valid how (? )    TOK Here  (?) "



  # GNU triplet for the compiler, e.g. i486-linux-gnu for Debian i386,
  # i686-linux-gnu for Ubuntu i386
  basename="`basename "$0"`"
  triplet="${basename%-pkg-config}"
  # Normalized multiarch path if any, e.g. i386-linux-gnu for i386
  multiarch="`dpkg-architecture -t"${triplet}" -qDEB_HOST_MULTIARCH 2>/dev/null`"
  # Native multiarch path
  native_multiarch="$(cat /usr/lib/pkg-config.multiarch)"

  # This can be used for native builds as well, in that case, just exec pkg-config "$@" directly.
  if [ "$native_multiarch" = "$multiarch" ]; then
    exec pkg-config "$@"
  fi





#setting triplet
  PKG_CONFIG_LIBDIR="/usr/local/${triplet}/lib/pkgconfig"

echo "@line $LINENO test triplet PKG_CONFIG_LIBDIR = $PKG_CONFIG_LIBDIR now set here  TOK  (?) "




  # For a native build we would also want to append /usr/local/lib/pkgconfig
  # at this point; but this is a cross-building script, so don't
  #setting share
        PKG_CONFIG_LIBDIR="$PKG_CONFIG_LIBDIR:/usr/local/share/pkgconfig"
echo "@line $LINENO test share PKG_CONFIG_LIBDIR = $PKG_CONFIG_LIBDIR now set here  TOK  (?) "






  if [ -n "$multiarch" ]; then
    PKG_CONFIG_LIBDIR="/usr/local/lib/${multiarch}/pkgconfig:$PKG_CONFIG_LIBDIR"
    PKG_CONFIG_LIBDIR="$PKG_CONFIG_LIBDIR:/usr/lib/${multiarch}/pkgconfig"
  fi
#mores stting
  PKG_CONFIG_LIBDIR="$PKG_CONFIG_LIBDIR:/usr/${triplet}/lib/pkgconfig"
echo "@line $LINENO test share PKG_CONFIG_LIBDIR = $PKG_CONFIG_LIBDIR more set (?)  TOK  (?) "



  # For a native build we would also want to append /usr/lib/pkgconfig
  # at this point; but this is a cross-building script, so don't
  # If you want to allow use of un-multiarched -dev packages for crossing
  # (at the risk of finding build-arch stuff you didn't want, if not in a clean chroot)
  # Uncomment the next line:
  # PKG_CONFIG_LIBDIR="$PKG_CONFIG_LIBDIR:/usr/lib/pkgconfig"
  # ... but on Ubuntu we rely cross-building with non-multiarch libraries:
  if dpkg-vendor --derives-from Ubuntu; then
      PKG_CONFIG_LIBDIR="$PKG_CONFIG_LIBDIR:/usr/lib/pkgconfig"
  fi
  PKG_CONFIG_LIBDIR="$PKG_CONFIG_LIBDIR:/usr/share/pkgconfig"

echo "@line $LINENO test share PKG_CONFIG_LIBDIR = $PKG_CONFIG_LIBDIR last set (?)  TOK  (?) "




#if [ $DEBUG -eq 1 }; then
echo "@line $LINENO export  PKG_CONFIG_LIBDIR = $PKG_CONFIG_LIBDIR  (?) "
#fi

# export
#  Marks each NAME for automatic export to the environment of subsequently
#  executed commands.  If VALUE is supplied, assign VALUE before exporting.
  export PKG_CONFIG_LIBDIR
# TOK echo "@line $LINENO # result of export PKG_CONFIG_LIBDIR trace  TOK (?) "
# TOK echo $?

fi


# Printing the data available in $@
for i
do
 echo "Argument  =  $i "
done

#echo "@line $LINENO entry print options passed  (?) "

# cannon be sure abyut the aerch patch here
#this  call doe snot return progress only when it fails returns -1  (?)

exec pkg-config "$@"

#never gets here
: '
echo "@line $LINENO # exit  exec  pkg-config trace TOK (?) "
echo $?
'


astrogeek 04-21-2019 01:33 PM

Quote:

Originally Posted by pan64 (Post 5986980)
probably you don't understand, but if you won't post the result we cannot explain.

Quote:

Originally Posted by AnneRanch (Post 5986991)
It is no necessary to tell me that I do not understand.
Using same attitude - it would help if you read the post. See attached.

Either way - so far nobody seems to be able to answer or guess why it does not work PART of the time.
It would be helpful to post replies actually answering the original question without getting sidetracked with references to man or doc

Pan64's comment, followed by further helpful suggestions to try to help you understand was perfectly reasonable.

Your reply is needlessly defensive and the only display of "attitude" in this thread.

Quote:

Originally Posted by AnneRanch (Post 5987189)
If it helps YOU , here it is.
Have a ball, but since the problem is solved keep in mind I am NOT interested in your analysis of this script and no longer subscribe to this thread.

Again, this is not a reasonable response to a reasonable request for additional information.

Everyone here volunteers their time and is making good faith effort to help with your problem. This is a friendly place for exchange of knowledge, and that is a two way exchange, please make effort to deal with others in that spirit, and in a less confrontational manner.

Please review the Site FAQ for guidance in posting your questions and general forum usage.


All times are GMT -5. The time now is 11:52 PM.