With my initial success using the CMOC compiler to build a CoCo program, I wanted to automate my steps (build and copy to disk image).
Back in my OS-9 days, I used an OS-9 shell script called “doit” that would manually do the steps to build whatever project it was designed for. This would do the compile, optimize, link, etc. steps for me.
I wanted to make a shell that let me specify the C file then build and copy it to some emulator disk image. I thought it might be like:
doit hello.c
I couldn’t remember how to pass parameters in to a shell script, so instead of doing a websearch, I just asked ChatGPT:
“how can you pass a parameter to a unix shell script?
To pass a parameter to a Unix shell script, you can use the
$1
,$2
,$3
, etc. variables, which correspond to the first, second, third, etc. arguments passed to the script. For example, if you want to pass a filename as a parameter to your script, you could use the following syntax:./myscript.sh myfile.txt
Then, in the script, you can access the parameter using the
$1
variable, like this:#!/bin/bash
# Print the filename passed as a parameter
echo "The filename is: $1"
Alternatively, you can use the
shift
command to access all of the arguments passed to the script, like this:#!/bin/bash
# Print all of the arguments passed to the script
echo "The arguments are: $@"
# Shift the arguments to the left, so that $1 now refers to the second argument, $2 now refers to the third argument, etc.
shift
# Print all of the arguments again, after shifting
echo "The arguments are now: $@"
In this example, the
– chat.openai.comshift
command is used to move all of the arguments to the left by one position, so that the second argument is now accessible using the$1
variable, the third argument is accessible using the$2
variable, and so on. This can be useful if you want to access all of the arguments passed to the script, but you don’t know in advance how many arguments will be passed.
That was quicker than a Google search! I decided to just tell it what I wanted to do:
Write a shell script that takes one parameter (a name) and then runs “cmoc name” and then “decb copy name.bin C.DSK,NAME.BIN -2r”
Here is an example of a Unix shell script that takes one parameter (a name) and then runs the
cmoc
anddecb
commands using that parameter:#!/bin/bash
# Get the name parameter
name=$1
# Run the cmoc command using the name parameter
cmoc $name
# Run the decb command using the name parameter
decb copy $name.bin C.DSK,$name.BIN -2r
In this script, the first line specifies the shell interpreter to use (in this case,
/bin/bash
), and the second line uses the$1
variable to retrieve the first argument passed to the script (i.e., the name parameter). The third and fourth lines use thecmoc
anddecb
commands, respectively, to perform the specified operations using the name parameter.To run this script, you would use the following command:
./myscript.sh myname
Replace
– chat.openai.commyscript.sh
with the name of your script file, andmyname
with the name you want to use as the parameter. The script will then run thecmoc
anddecb
commands using the specified name.
Well, if it can do that, maybe it an make it even better…
update that script so the parameter’s extension is removed and replaced with .bin for the copy
To update the script so that the extension of the parameter is removed and replaced with
.bin
for thedecb copy
command, you can use thebasename
anddirname
commands to extract the base name and directory path of the parameter, respectively, and then use thesed
command to remove the extension from the base name. Here is an example of how you could do this:#!/bin/bash
# Get the name parameter name=$1
# Use the basename and dirname commands to extract the base name and directory path of the parameter
dir=$(dirname $name) base=$(basename $name)
# Use the sed command to remove the extension from the base name
name_without_ext=$(echo $base | sed -e "s/\.[^.]*$//")
# Run the cmoc command using the name parameter
cmoc $name
# Run the decb copy command using the modified name
decb copy $name_without_ext.bin $dir/C.DSK $name_without_ext.BIN -2r
In this updated script, the
basename
anddirname
commands are used to extract the base name and directory path of the parameter. Thesed
command is then used to remove the extension from the base name, and the modified name is used as the argument to thedecb copy
command.To run this script, you would use the same command as before:
./myscript.sh myname
This time, however, the
– chat.openai.comdecb copy
command will use the modified name (i.e., the name with the extension removed and replaced with.bin
) instead of the original name. For example, if you passmyfile.txt
as the parameter, thedecb copy
command will usemyfile.bin
as the destination filename.
And maybe even more better than that!
can you uppercase the second $name_without_ext in the decb line?
To uppercase the second instance of
$name_without_ext
in thedecb
line, you can use thetr
command to convert the string to uppercase. Here is an example of how you could do this:#!/bin/bash # Get the name parameter name=$1 # Use the basename and dirname commands to extract the base name and directory path of the parameter dir=$(dirname $name) base=$(basename $name) # Use the sed command to remove the extension from the base namename_without_ext=$(echo $base | sed -e "s/\.[^.]*$//") # Use the tr command to convert the name to uppercase name_upper=$(echo $name_without_ext | tr "[:lower:]" "[:upper:]") # Run the cmoc command using the name parameter cmoc $name # Run the decb copy command using the modified name decb copy $name_without_ext.bin $dir/C.DSK,$name_upper.BIN -2rIn this updated script, the
tr
command is used to convert the$name_without_ext
variable to uppercase, and the resulting string is stored in the$name_upper
variable. The$name_upper
variable is then used as the second argument to thedecb copy
command, so the second instance of the name will be uppercase.To run this script, you would use the same command as before:
./myscript.sh myname
This time, the
decb copy
command will use the uppercase version of the name as the second argument. For example, if you passmyfile.txt
as the parameter, thedecb copy
command will be run as follows:decb copy myfile.bin mydir/C.DSK,MYFILE.BIN -2r
Notice that the second instance of the name (i.e.,
– chat.openai.comMYFILE.BIN
) is now in uppercase.
It worked great! But it didn’t tell me what it was doing, so…
Make it print what is happening at the cmoc and decb steps
To make the script print what is happening at the
cmoc
anddecb
steps, you can simply add someecho
statements to the script. Here is an example of how you could do this:
#!/bin/bash
# Get the name parameter
name=$1
# Use the basename and dirname commands to extract the base name and directory path of the parameter
dir=$(dirname $name) base=$(basename $name)
# Use the sed command to remove the extension from the base name
name_without_ext=$(echo $base | sed -e "s/\.[^.]*$//")
# Use the tr command to convert the name to uppercase name_upper=$(echo $name_without_ext | tr "[:lower:]" "[:upper:]")
# Print a message before running the cmoc command
echo "Running cmoc on $name..."
# Run the cmoc command using the name parameter
cmoc $name
# Print a message before running the decb copy command
echo "Running decb copy on $name_without_ext.bin..."
# Run the decb copy command using the modified name
decb copy $name_without_ext.bin $dir/C.DSK,$name_upper.BIN -2rIn this updated script, two
echo
statements have been added to print a message before running thecmoc
anddecb copy
commands, respectively. These messages will be printed to the console and will indicate what is happening at each step of the script.To run this script, you would use the same command as before:
./myscript.sh myname
When you run the script, you should see the following output:
Running cmoc on myname...
(output from cmoc command)
Running decb copy on myfile.bin...
(output from decb copy command)
The output from the
– chat.openai.comcmoc
anddecb copy
commands will be shown after the messages indicating what is happening at each step.
And that’s how I got this…
I will never write a shell script again…
Now have it write a Makefile — so much better!
Now I’m gonna try that…! One of the first examples I saw someone using it for was creating a regular expression. If it can do that and figure out Excel spreadsheet macros, I think we’ve solved most the worlds problems.
The shell script is dangerous if filename have a space (or special shell characters) inside … always do proper quoting in shell scripts! It has to be better written this way:
name=”$1″
cmoc “$name”
BTW: Makefile writing handling spacy filenames correctly could be also a horror. ;)
“Be sure to quote all filename variables” and I expect will will do that.