EDIT: I've pushed my code to a new branch just for this question
I'm trying to create a bash file that loops over all directories (and subdirectories) of a predefined structure (i.e. I know for certain the directories are named in a specific manner).
An obvious solution for this was a case...esac
statement using regular expression patterns. So off I go to StackOVerflow where I found this post explaining how to do exactly what I need, so I'm ready to create a test code and this is what I came up with:
test
|
|__ python
|
|__ python-
|
|__ python-a
|
|__ python-0
|
|__ python0-
|
|__ python2
|
|__ pythona
|
|__ test.sh
and run this:
# test.sh
#!/bin/bash
shopt -s extglob;
for dir in $(ls); do
case $dir in
python*(-)*([a-zA-Z0-9]))
echo "yes -> $dir"
;;
*)
echo "no -> $dir"
;;
esac
done
shopt -u extglob;
which gives me the following output:
yes -> python
yes -> python-
yes -> python-a
no -> python0-
yes -> python2
yes -> pythona
no -> test.sh
which seems to work fine.
I carried this method on to my actual code:
# actual_code.sh
cd $PROGRAMS_DIR
for language in $(ls -l --group-directories-first | tail -n $(ls -l | awk '{print $1}' | grep d | wc -l) | awk '{print $9}'); do
cd $language
echo "language -> $language"
for algorithm in $(ls -l --group-directories-first | tail -n $(ls -l | awk '{print $1}' | grep d | wc -l) | awk '{print $9}'); do
cd $algorithm
echo "algo -> $algorithm"
shopt -s extglob;
case $language in
rust*(-)*([0-9]))
rustc "${algorithm}_run.rs" -o "${algorithm}_run"
COMMAND="./${algorithm}_run"
if [ $TEST -eq 1 ]; then
echo "> Running Rust tests for $algorithm"
rustc --test "${algorithm}_test.rs" -o "${algorithm}_test"
./${algorithm}_test
if [ $(echo $?) -ne 0 ]; then
exit 1
fi
fi
;;
go*(-)*([0-9]))
go build -o "${algorithm}_run" .
COMMAND="./${algorithm}_run"
if [ $TEST -eq 1 ]; then
echo "> Running Go tests for $algorithm"
go test
if [ $(echo $?) -ne 0 ]; then
exit 1
fi
fi
;;
java*(-)*([0-9]))
javac -cp .:$JUNIT:$HAMCREST *.java
COMMAND="java -cp .:${JUNIT}:${HAMCREST} ${algorithm}_run"
if [ $TEST -eq 1 ]; then
echo "> Running Java tests for $algorithm"
java -cp .:${JUNIT}:${HAMCREST} ${algorithm}_test
if [ $(echo $?) -ne 0 ]; then
exit 1
fi
fi
;;
c*(-)*([0-9]))
# TODO: Try to implement both the normal executable and the -O2 optimisation
gcc -Wall -c "${algorithm}.c" "${algorithm}_run.c"
gcc -o "${algorithm}_run" "${algorithm}.o" "${algorithm}_run.o"
COMMAND="./${algorithm}_run"
if [ $TEST -eq 1 ]; then
echo "> Running C tests for $algorithm"
gcc -Wall -c "${algorithm}.c" "${algorithm}_test.c" $UNITY
gcc -o "${algorithm}_test" "${algorithm}.o" "${algorithm}_test.o" "unity.o"
./${algorithm}_test
if [ $(echo $?) -ne 0 ]; then
exit 1
fi
fi
;;
python*(-)*([0-9]))
COMMAND="python ${algorithm}_run.py"
if [ $TEST -eq 1 ]; then
echo "> Running Python tests for $algorithm"
pytest .
if [ $(echo $?) -ne 0 ]; then
exit 1
fi
fi
;;
haxe*(-)*([0-9]))
COMMAND="haxe --main ${algorithm^}_Run.hx --interp"
if [ $TEST -eq 1 ]; then
echo "> Running Haxe tests for $algorithm"
haxe --main "${algorithm^}_Test.hx" --library utest --interp -D UTEST_PRINT_TESTS
if [ $(echo $?) -ne 0 ]; then
exit 1
fi
fi
;;
*)
echo "($language) has no compilation steps. Did you forget to update the benchmark script?"
;;
esac
shopt -u extglob;
.
.
.
some other random code
.
.
.
done
done
Executing this code now gives me this
./actual_code.sh: line 408: syntax error near unexpected token `('
./actual_code.sh: line 408: ` rust*(-)*([0-9]))'
I'm obviously missing something, but they look the same to me. Also, the echo
part doesn't work. It goes straight to the error which is weird as this is an interpreted language.