Bash Scripting: I currently am supposed to have 500 files inside a directory, how can I stop a bash script if any files are missing? - bash

I currently have a directory that is supposed to have 500 files. Each file is of the name form List.1.rds, ... List.500.rds. The way I can see which ones are missing is by the following code in bash:
for((i=1; i<=500; i++)); do name="List.${i}.rds"; [[ ! -e "$name" ]] && echo "missing $name"; done
If a file is missing, it returns the missing file name. However, I would like to go one step further and stop the entire script should any file be missing. Is there a way to do this? thanks.

It can be as simple as setting a flag when a file is missing:
miss=0
for ((i=1;i<=500;i++)); do
file=List.$i.rds
if [[ ! -e $file ]]; then
echo "Missing $file"
miss=1
fi
done
# exit if "miss" flag is 1
((miss)) && exit 1

Related

Checking for multiple files with bash script

I have a bash file where I am currently using an if statement to check for two files existing before continuing with the rest.
if [[ -e $flchck ]] && [[ -e $flchck2 ]]; then
--do stuff here
else
--sends me an email telling me the files were not found.
My question is, is this the most efficient way to do this? If I need it to check more files, would I just keep adding && to include more.
What if I want it to tell me which file(s) was missing when it did this check.
Any guidance/direction on how to handle this would be greatly appreciated.
Typically one would use an array for this purpose. Assuming that your error-report-mailing code is encapsulated in a command named send_error_email, this may look like:
#!/usr/bin/env bash
files=( /path/to/file1 /path/to/file2 /path/to/file3 )
for file in "${files[#]}"; do
if ! [[ -e $file ]]; then
send_error_email "$file does not exist"
exit 1
fi
done
# do stuff here

I am trying to find a file in a directory, but the shell code snippet is not detecting it

I have a shell script, which has a code block to find a file in the directory but it is not doing the job. I have a file in my directory "changeip.data", in one of the block i am tring to find the file, but it is not detecting the file. I want to check with the condition that the filename starts with "change".
cd /home/raymond
file = "changeip.data"
if [[ "$file" == change* ]]; then
echo "File found..."
else
echo "File not found..."
fi
Can somebody help me with this?
the problem in your code is extra space. make sure there is no space while pointing to file name. refer the following initilization:
file="changeip.data"
EDIT:
#==================
Full code:
#==================
file="changeip.data"
if [[ "$file" == change* ]]; then
echo "File found..."
else
echo "File not found..."
fi
The issue is that to check is a file exists, you need to use -e but this will only work with complete file names without globbing.
As an alternative, you could try the following:
if [[ "$(find -maxdepth 1 -name "zipp*pm2" | wc -l)" -gt "0" ]];
then
echo "File Found";
else
echo "File Not Found";
fi
This will count the output from the find command and if it is greater than 0, will return "File Found", otherwise "File Not Found"

bash script with if [[ ! -f path_to_files ]]

On MacOS Catalina, I have a bash script with
if [[ ! -f $CR/home/files/Recovery_*.txt ]]
then
echo "File does not exists in /home/files directory. Exiting" >> $log
echo "Aborted - $CR/home/files/Recovery_*txt not exist"
exit
fi
Even though there are 2 files in the directory the script exits. If I list directory contents beforehand there are 2 files. If I change it as follows the if is skipped:
if [[ `ls -la $CR/home/files/Recovery_*.txt | wc -l` -eq 0 ]]
then
echo "No files are :"
exit
fi
I am wanting to use the if -f conditional.
Any suggestions please?
Cheers, C
If you use nullglob, a glob expression that doesn't match returns an empty string. This lets us count files in bash without spawning other processes. Create an array with the expression, then check its length.
shopt -s nullglob
files=($CR/home/files/Recovery_*.txt)
if [[ ${#files[#]} -eq 0 ]]
then
echo "No files"
exit
fi
[Edited]
The error was not the variables, but the missing shebang as the script has come across from W2K3 SFU.
The tip about shellchecker.net was awesome and I will use that from now.
Thanks.

Can not locate a file in a bash script?

I am running a shell script which expects a file in a directory but I can not seem to locate it.
This is my shell script
#!/usr/bin/env bash
# expects a file to be present in current directory called INPUT_FILE
# this is the value set in the jenkins config
if [ ! -f INPUT_FILE ] ;then
echo "file ${INPUT_FILE} does not exist"
exit 1
fi
And in windows I am running a shell script from a directory like this
D:\scripts> ./all/script.sh
I tried to put INPUT_FILE.csv in scripts folder and also in all folder but it does not seem to work. Where should this file be present?
Make sure you have the correct filename.
Windows hides the extension of a file, when you look for INPUT_FILE.csv use
if [ ! -f INPUT_FILE.csv ] ;then
echo "file INPUT_FILE.csv does not exist"
exit 1
fi
You tried to use the variable INPUT_FILE. When you want this, try
input_file="INPUT_FILE.csv"
if [ ! -f "${input_file}" ]; then
echo "file ${input_file} does not exist"
exit 1
fi
Also note that bash is case sensitive, so INPUT_FILE.csv is different from input_file.csv.
It's better to list files first to verify the extension and all file names in the directory then check for the existence of the files.
#!/usr/bin/env bash
ALL_FILES=$(ls -I "*.sh") # This will list all file except .sh
echo "Files in current Directory $ALL_FILES"
INPUT_FILE="INPUT_FILE" # or INPUT_FILE="INPUT_FILE.csv"
if [ ! -f $INPUT_FILE ] ;then
echo "file ${INPUT_FILE} does not exist"
exit 1
else
echo "file exist ${INPUT_FILE}"
exit 0
fi
The first advantage, It will list all the file and the second one, If exist you will get the output.

Treating space as newline character in bash

I have written a bash just to display the name of all the files of a given directory but when I am running this it breaking the file name which has spaces.
if [ $# -eq 0 ]
then
echo "give a source directory in the command line argument in order to rename the jpg file"
exit 1
fi
if [ ! -d "$1" ]; then
exit 2
fi
if [ -d "$1" ]
then
for i in $(ls "$1")
do
echo "$i"
done
fi
I am getting the following thing when I run the bash script
21151991jatinkhurana_image
(co
py).jpg
24041991jatinkhurana_im
age.jpg
35041991jatinkhurana_image
.jpg
The thing that i have tried till now is resetting the IFS variable like IFS=$(echo -en "\t\n\0") but found no change....
If anyone know please help me.....
Do not loop through the result of ls. Parsing ls makes world worse (good read: Why you shouldn't parse the output of ls).
Instead, you can do make use of the *, that expands to the existing content in a given directory:
for file in /your/dir/*
do
echo "this is my file: $file"
done
Using variables:
for file in $dir/*
do
echo "this is my file: $file"
done

Resources