I have this working and simple code to echo positional parameters VALUES
for i
do
echo "Argument = $i "
done
Here is the actual output from the scrip
Argument = --atleast-pkgconfig-version
Argument = 0.9.0
It does the job, but I have been trying to figure out how to ADD an index to the echo output.
This does not work
cnt=1
for i
do
echo "Argument $cnt = $i "
((cnt++))
done
The current code does the job, the index would be just icing on the cake - like this:
Argument #1 = --atleast-pkgconfig-version
Argument #2 = 0.9.0
Any ideas?
Update #1
I am posting this as a copy from text editor, basically to bypass „editing in 5 minutes „ time limit.
I did run both codes, both in #! /bin/sh scripts. One is run directly in main script, the second is in script called from the main – as indicated. Both codes run fine in MAIN script, but the first code, the one I have originally posted as having an issue, does not run in secondary script.
Yes, the “more portable” code solves the issue – it runs correctly in desired script. It still does not answer the original question – why does it fail with “not found” error.
MAIN script--exists glib-2.0 0
--modversion glib-2.0 2.54.3
--exists --print-errors glib-2.0 >= 2.28 0
First code - OK
Argument #1 = dummy
Argument #2 = mt
Second code OK
Argument #1 = dummy
Argument #2 = mt
Secondary script
First code – FAILS
/usr/bin/arm-linux-gnueabihf-pkg-config: 19: /usr/bin/arm-linux-gnueabihf-pkg-config: cnt++: not found
Argument #1 = --print-errors
/usr/bin/arm-linux-gnueabihf-pkg-config: 19: /usr/bin/arm-linux-gnueabihf-pkg-config: cnt++: not found
Argument #1 = glib-2.0 >= 2.28
/usr/bin/a
Second code – runs fine
Argument #1 = --exists
Argument #2 = --print-errors
Argument #3 = glib-2.0 >= 2.28
First code
cnt=1
for i do
echo "Argument #$cnt = $i"
((cnt++))
done
Second code
count=1
for arg do
printf 'Argument #%d = %s\n' "$count" "$arg"
count=$(( count + 1 ))
done
j=0; for i do printf "arg#$((j=j+1)) = %s\n" "$i"; done. And no, you cannot use++jin all shells.