This Bash script provides a convenient way to compile, run, and optionally delete a Rust executable. It supports various options, including running in a temporary directory, watching for file changes, sending desktop notifications, and installing the script to the user's local ~/bin
directory.
The script compiles and runs a given Rust file. If the compilation and execution are successful, it deletes the generated executable.
Example:
./compile_and_run_rust.sh intro2.rs
The -t
or --temp
option allows the script to compile and run the Rust file in a temporary directory. This is useful to avoid cluttering your working directory with executables.
Example:
./compile_and_run_rust.sh -t intro2.rs
The -w
or --watch
option enables a watch mode. In this mode, the script monitors the specified Rust file for changes. If the file is modified, the script will re-compile, run, and optionally delete the executable. The script uses inotifywait
for efficient file monitoring, and falls back to sleep and checksum if inotifywait
is not available.
Example:
./compile_and_run_rust.sh -w intro2.rs
The -n
or --notify
option enables desktop notifications. A notification with low urgency is sent upon successful compilation, and a notification with critical urgency is sent upon compilation failure.
Example:
./compile_and_run_rust.sh -n intro2.rs
The --install
option installs the script to the ~/bin
directory. If ~/bin
is not in your PATH
, the script provides instructions to add it.
Example:
./compile_and_run_rust.sh --install
The -h
or --help
option displays a help text that explains how to use the script and its options. The help text is colorized for better readability.
Example:
./compile_and_run_rust.sh -h
You can pipe a Rust file into the script. This is useful when you want to quickly run a Rust file without specifying its name.
Example:
cat intro2.rs | ./compile_and_run_rust.sh
Here's the full script:
See compile_and_run_rust.sh file.
curl -O https://gist.githubusercontent.com/abhisekp/c4625eb8f324991957520c8a7c1e15cb/raw/497cca2d7bc310821bccc55e86880369f7013648/compile_and_run_rust.sh && chmod +x compile_and_run_rust.sh && ./compile_and_run_rust.sh
- Bash
inotifywait
for watch mode (optional)notify-send
for notifications (optional)
- Save the script into a file, for example,
compile_and_run_rust.sh
. - Make the script executable:
chmod +x compile_and_run_rust.sh
.
Run the script with the desired options and the Rust file as an argument. For example, to compile, run, and clean intro2.rs
in a temporary directory while watching for changes and sending notifications:
./compile_and_run_rust.sh -t -w -n intro2.rs