Caveat: Your tags "linux
shell
" do not specify a specific shell. POSIX sh, the standard specifying minimum required behavior for /bin/sh
, does not support arrays; you should use a specific shell, such as bash or ksh, which does. To do this, you need to start your script with an appropriate shebang (such as #!/bin/bash
instead of #!/bin/sh
), and do any manual invocations with the correct shell (so bash -x myscript
if you would otherwise use sh -x myscript
... though if you've set the shebang correctly and have +x
permissions, you can always just ./myscript
)
# this is broken
FILES=PTscalar_1.0/mibenchforpt/security/sha/*.s
...does not create an array.
# this works in bash, ksh, and zsh
files=( PTscalar_1.0/mibenchforpt/security/sha/*.s )
does create an array, which can be expanded as "${files[@]}"
. So:
# this works in bash and ksh, and probably zsh
for file in "${files[@]}"; do
...
done
However, in this particular case, you don't have a reason to use an array at all:
# this works with absolutely any POSIX-compatible shell
for file in PTscalar_1.0/mibenchforpt/security/sha/*.s; do
echo "$sfile"
objectFile=${sfile%.s}.o
exefile=${objectFile%.o}.ex
simplescalar/bin/sslittle-na-sstrix-as -o "$objectFile" "$sfile"
done
Note a few corrections made in the above:
- The right-hand-side of assignments in with no literal whitespace in their syntax do not need to be quoted.
- All expansions (such as
$objectFile
) do need to be quoted, so, "$objectFile"
.
- ...yes, this does include
echo
; to test this, run s='*'
and compare the output of echo $s
to echo "$s"
.
To address the follow-up question you edited in:
ofiles=( PTscalar_1.0/mibenchforpt/security/sha/*.o )
simplescalar/bin/sslittle-na-sstrix-ld -o "$exefile" "${ofiles[0]}" "${ofiles[1]}"
...is a literal answer, but this would need to be edited if you had two or more outputs. Much better to do it this way instead:
ofiles=( PTscalar_1.0/mibenchforpt/security/sha/*.o )
simplescalar/bin/sslittle-na-sstrix-ld -o "$exefile" "${ofiles[@]}"