Code below is respoonsible for checking home Directory. Now instead of one Directory I…
- User
Code below is respoonsible for checking home Directory. Now instead of one Directory I want to have 4 of them in separate file. How to do so? #!/usr/bin/env fish
Set the default directory if no argument is provided
set DIRECTORY /home/wovri
Find all .git directories under the given path
for git_dir in (find DIRECTORY -type d -name .git) # Get the directory containing the .git folder set repo_dir (dirname "git_dir")
# Check if there are unstaged changes if git -C "$repo_dir" status --porcelain | grep -q '^.[^ ]' echo "Warning: Unstaged changes found in $repo_dir" endend
Assistant
Of course! To check multiple directories listed in a separate file, you need to modify the script to read that file and loop through each directory it contains.
Here is the step-by-step solution.
Step 1: Create the File with Your Directories
First, create a new file that will hold the paths to the four (or more) directories you want to scan. Let's name it
directories_to_check.txtand place it in the same folder as your script.Each directory path should be on a new line. You can also add comments with
#and empty lines, which the script below will ignore.directories_to_check.txt:# My main development folders /home/wovri/personal_projects /home/wovri/work_projects # Web server directories /var/www/html # Another important location /etc/my-app-configsStep 2: Update the Fish Script
Now, modify your fish script to read
directories_to_check.txt, loop through each path, and run the git check.Here is the new and improved script:
check_git_status.fish:#!/usr/bin/env fish # The file containing a list of directories to check, one per line. set CONFIG_FILE "directories_to_check.txt" # Check if the configuration file exists before proceeding. if not test -f "$CONFIG_FILE" echo "Error: Configuration file not found at '$CONFIG_FILE'" echo "Please create it with one directory path per line." exit 1 end echo "Scanning for unstaged changes in directories listed in $CONFIG_FILE..." # Read each line from the config file. for base_dir in (cat "$CONFIG_FILE") # Trim whitespace from the line. set base_dir (string trim "$base_dir") # Skip empty lines or lines that start with a '#' (comments). if test -z "$base_dir" -o (string match -q "#*" "$base_dir") continue end # --- Start of original logic --- # Find all .git directories under the current base_dir for git_dir in (find "$base_dir" -type d -name .git 2>/dev/null) # Get the directory containing the .git folder set repo_dir (dirname "$git_dir") # Check if there are unstaged changes if git -C "$repo_dir" status --porcelain | grep -q '^.[^ ]' echo "Warning: Unstaged changes found in $repo_dir" end end # --- End of original logic --- end echo "Scan complete."How to Use It
- Save the script above as
check_git_status.fish. - Save the directory list as
directories_to_check.txtin the same folder. - Make the script executable:
chmod +x check_git_status.fish. - Run the script:
./check_git_status.fish.
Explanation of Changes
-
Configuration File:
set CONFIG_FILE "directories_to_check.txt": We define a variable for our new file. This makes it easy to change the filename later.
-
Error Handling:
if not test -f "$CONFIG_FILE": This is a safety check. The script will now exit with a helpful error message if it can't find thedirectories_to_check.txtfile.
-
The Main Loop:
for base_dir in (cat "$CONFIG_FILE"): This is the key change. Instead of a single hardcoded directory, we now have aforloop that iterates over each line from the output ofcat "$CONFIG_FILE".- Each line (a directory path) is assigned to the
base_dirvariable for each iteration of the loop.
-
Robustness (Handling Comments and Empty Lines):
set base_dir (string trim "$base_dir"): Removes any leading/trailing whitespace.if test -z "$base_dir" -o (string match -q "#*" "$base_dir"): This line is a great addition for user-friendly configuration. It checks if the line is either empty (-z) or starts with a#. If so, itcontinues to the next line, effectively ignoring comments and blank lines.
-
Integrating the Original Logic:
- The entire logic from your original script is placed inside this new
forloop. - The
findcommand was changed fromfind $DIRECTORYtofind "$base_dir"to use the directory from the current line of the file. 2>/dev/nullwas added to thefindcommand to suppress "Permission denied" errors for directories the script can't read.
- The entire logic from your original script is placed inside this new
- Save the script above as
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.