The script has a bug. The ./
on each line forces you to cd
to the directory where these files live before it will work correctly.
Perhaps you could change it with something like
sed 's%\./%/path/to/dir/%' script.sh | sh
The sed
command prints a modified version of the script to its standard output, and we feed that to another sh
instance to execute it. (Maybe at first take out the pipe to sh
so you can see what it's printing, and of course, modify the replacement string to point to the correct directory.)
Some sed
variants allow you to use an -i
option to write back the changes to the file instead of print them, if you should want to make this change permanent.
Generally, a correctly written script should run in any directory, and not hardcode any paths. Without more knowledge about your specific use case, it's hard to say. The faulty script would obviously be a lot more elegant if it used a loop, so there would only be one place to change; and maybe then you could make that a variable you could change at run time without modifying the script, by reading a configuration file or accepting a command-line parameter. Or maybe it should simply do
cd "$(dirname "$0")"
at the top, to encapsulate and shield the user from the requirement to run in a particular directory. (This would only change the working directory for the duration of the script; when it exits, the parent shell regains control, and of course remains in the directory where you started.) Another trick is to add the current directory to the front of the PATH
, though this is perhaps slightly dubious in this case.
PATH="$(dirname "$0"):$PATH"
The loop could simply be
while IFS= read -r speed gain frob offset luck charm; do
./exec30_10 "$speed" "$gain" "$frob" "$offset" "$luck" "$charm"
done <<\____HERE
55000 6234611 41 0 -4 1
55000 6234611 41 0 -3 1
55000 6234611 41 0 -2 1
55000 6234611 41 0 -1 1
55000 6234611 41 0 0 1
____HERE
which also somewhat documents what the parameters mean through the variable names (though obviously I just had to guess wildly because I have no idea). Or if your example is representative and most of these are not variable, perhaps
# Bash-only syntax
for((i=-4; i<=0; ++i)); do
./exec30_10 55000 6234611 41 0 "$i" 1
done
Here, I have assumed that you used cd "$(dirname "$0")"
at the top of the script (and so the ./
before the executable's name can and should remain). (The variable $0
is the name of the script itself, with any leading path intact if the caller specified one.)