Skip to content
Advertisement

Shell Script file access

I have written a shell script on my mac. Following code:


    echo [objects] > temp.prj
    echo $1.obj >> temp.prj
    chmod 777 temp.prj
    wla-65816 -o $1.asm $1.obj
    wlalink -vr temp.prj $1.fig
    rm $1.obj
    rm temp.prj

The problem is that the program wlalink gives me an error message:

LOAD_FILES: Could not open file “temp.prj”.

It seems that there are problems with some rights. The script is creating a file and there is the right content in the file. Same with running sudo.

Any ideas?

Advertisement

Answer

The error you’ve described doesn’t sound like something the shell would produce, but the only tool that appears to load the file in question is wlalink. If you step through the lines of the script, typing each one at a shell prompt, does the same error appear? If so, after which line?

That aside, there are a number of quality issues with your script which you should fix, the result of which might make things easier to debug or predict.

  1. Use shebang. There are multiple shell interpreters on your computer, and when writing a script, it’s best to specify the one you want to interpret your script. Add a line like #!/bin/sh as the very first line of the script.
  2. Quote your variables and any special characters. Square brackets, for example, are used as part of “pattern matching“. If you want to make sure your string will be taken literally, use literal (single) quotes, i.e. echo '[objects]' > temp.prj
  3. Test for success. If any step in your script fails the script blindly continues running commands until the end, even though they’re guaranteed to fail. Add set -e near the top of the script for a simple version of this.
  4. Don’t use silly permissions. chmod 777 means that the file can be read, written to and executed by the world. This is not an executable file. And it certainly does not need to be world writable. Think about what you need, and set your file permissions to no more than that.

After a cleanup, and especially with some basic error handling, debugging will no doubt be easier.

#!/bin/sh

if [ -z "$1" ]; then
    echo "ERROR: I need an option." >&2
    exit 1
fi

# Debugging
set -e    # stop on error
set -x    # display our commands

echo "[objects]" > temp.prj
echo "$1.obj" >> temp.prj
chmod 640 temp.prj
wla-65816 -o "$1.asm" "$1.obj"
wlalink -vr temp.prj "$1.fig"
rm -f "$1.obj" temp.prj
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement