Answer the question
In order to leave comments, you need to log in
How to properly initialize an array in a shell script?
Wrote a small script to search for scripts in a given folder and run them. At startup, it swears at parentheses in the array initialization line.
The script itself:
#!/bin/sh
scripts_path="/home/anatoliy/scripts/test/"
scripts_names=( $(ls -a $scripts_path | egrep '\.sh$') )
for i in "${scripts_names[@]}"; do
echo "=== Start script $i ==="
sh $scripts_path$i
echo "=== End script $i"
done
test.sh: 4: test.sh: Syntax error: "(" unexpected
Answer the question
In order to leave comments, you need to log in
I solved the problem not by creating an array in a variable, but by searching for scripts in for.
#!/bin/sh
scripts_path="/home/anatoliy/scripts/test/"
for i in $(ls -a $scripts_path | egrep '\.sh$'); do
echo "=== Start script $i ==="
sh $scripts_path$i
echo "=== End script $i"
done
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question