how to work at the same time with multiple files inside file name array in linux shell script?

StackOverflow https://stackoverflow.com/questions/20144278

  •  03-08-2022
  •  | 
  •  

Frage

I have a shell script and i read all .s files in the specified folder first and then compile them to object file with a loop and after that link them to executable file. this:

FILES=PTscalar_1.0/mibenchforpt/security/sha/*.s

for sfile in $FILES 
do

echo "------------------------------------------------"
    echo $sfile
    objectFile="${sfile%.s}.o"
    exefile="${objectFile%.o}.ex"

    simplescalar/bin/sslittle-na-sstrix-as -o  $objectFile $sfile

done

but I have a problem: in sha mibench program we have 2 files that each of them is in this flow:

.c -> .s -> .o

but at the last stage two .o files should be linked into one executable file.

how I can get two file names at the same time and create a command to link them.

main code is this:

simplescalar/bin/sslittle-na-sstrix-ld -o __sha.ex _sha.o _sha_driver.o

is there any way to see inside of FILES like this:

OFILES=PTscalar_1.0/mibenchforpt/security/sha/*.o
simplescalar/bin/sslittle-na-sstrix-ld -o $exefile OFILES[0] OFILES[1]

and after that doing that in a loop for all files with this pattern first file is like *.o or *_main.o second is: *_driver.o

Thanks

War es hilfreich?

Lösung 3

I created this file and it worked:

#!/bin/bash
#compile to assembly:

FILES=*_driver.s

for sdriverfile in $FILES 
do

echo "------------------------------------------------"
# s file
    echo $sdriverfile
    sfile="${sdriverfile%_driver.s}.s"
    echo $sfile

# object files
    obj="${sfile%.s}.o"
    obj_driver="${sdriverfile%.s}.o"

#exe file
    exefile="${sfile%.s}_as.ex"
    echo $exefile

#compile
    /home/mahdi/programs/simplescalar/bin/sslittle-na-sstrix-as -o  $obj $sfile
    /home/mahdi/programs/simplescalar/bin/sslittle-na-sstrix-as -o  $obj_driver $sdriverfile

#link
    /home/mahdi/programs/simplescalar/bin/sslittle-na-sstrix-ld -o $exefile $obj $obj_driver -L /home/mahdi/programs/simplescalar/sslittle-na-sstrix/lib -lc -L /home/mahdi/programs/simplescalar/lib/gcc-lib/sslittle-na-sstrix/2.7.2.3/ -lgcc



done

thanks for answers.

Andere Tipps

Obviously this is possible in shell. However many people find that the make utility is better for building software than shell scripts simply because of these dependencies. take a look at GNU Make. Its documentation contains numerous examples of what you're trying to do.

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[@]}"
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top