Okay, you want to make your script more robust by checking if the applications exist before trying to launch them. This prevents errors if an application isn't installed or available in the system's PATH.
Here are a few ways to do this, from simplest to slightly more advanced:
Method 1: Using command -v (Recommended)
command -v is a Bash built-in that checks if a command exists and is executable. It's generally preferred over which because it's a shell built-in and handles aliases correctly.
#!/bin/bash
set -u # Exit if script tries to use an uninitialized variable
# Function to check and start an application
start_if_exists() {
local app_name="$1"
# Check if the command exists in PATH. Redirect output/error to /dev/null
if command -v "$app_name" > /dev/null 2>&1; then
echo "Starting $app_name..."
"$app_name" & # Execute the command in the background
else
echo "Warning: '$app_name' not found. Skipping."
fi
}
# --- Start launching applications with checks and delays ---
start_if_exists flameshot
sleep 1
start_if_exists volumeicon
sleep 2
start_if_exists nm-applet
sleep 2
start_if_exists pcmanfm
sleep 2
start_if_exists gexec
sleep 2 # Keep the last sleep if desired
echo "Script finished."
Explanation:
#!/bin/bash: Specifies the script should be run with Bash.
set -u: A good practice; it makes the script exit if it encounters a variable that hasn't been set.
start_if_exists(): A function is defined to avoid repeating the check logic.
local app_name="$1": Takes the first argument passed to the function (the application name) and stores it in a local variable. Using quotes ("$1") is important if app names ever contained spaces (though unlikely for executables).
command -v "$app_name": Checks if the command exists.
> /dev/null 2>&1: This is crucial.
> /dev/null: Redirects standard output (stdout) from command -v to /dev/null (which discards it). We don't need to see the path it finds.
2>&1: Redirects standard error (stderr) to the same place as standard output (/dev/null). If the command isn't found, command -v might write to stderr, and we want to suppress that too.
if ...; then ... else ... fi: Standard Bash conditional structure. The if checks the exit status of the last command in the condition (command -v ...). If command -v finds the command, it exits with status 0 (success), and the then block runs. Otherwise, it exits non-zero (failure), and the else block runs.
"$app_name" &: If found, the application is launched in the background using &.
sleep N: The delays remain between the attempted starts.
Method 2: Using which (Alternative)
which is a common utility that does a similar job to command -v.
#!/bin/bash
set -u
start_if_exists() {
local app_name="$1"
# Check if the command exists using 'which'
if which "$app_name" > /dev/null 2>&1; then
echo "Starting $app_name..."
"$app_name" &
else
echo "Warning: '$app_name' not found. Skipping."
fi
}
# --- Start launching applications with checks and delays ---
start_if_exists flameshot
sleep 1
start_if_exists volumeicon
sleep 2
start_if_exists nm-applet
sleep 2
start_if_exists pcmanfm
sleep 2
start_if_exists gexec
sleep 2
echo "Script finished."
This works identically to Method 1 in practice for most cases.
How to Use:
- Save the code to a file, for example,
start_apps.sh.
- Make it executable:
chmod +x start_apps.sh
- Run it:
./start_apps.sh
Choose the method you prefer (Method 1 using command -v is generally the most robust). This will prevent errors and provide warnings if an application isn't available.