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.
Related
I'm creating a bash script, but it does not seem to check if a folder exists, when it's based on variables. Although the folder does exists, when I cd into it.
#!/usr/bin/env bash
VAR1="/Users/nameuserhere/Desktop/";
VAR2=`date "+%Y-%m-%d"`;
VAR3="$VAR1$VAR2";
echo "folder path: $VAR3";
if [[ -f "$VAR3" ]]
then
echo "this/not does exists"
else
echo "this/not does not exist"
fi
Use -d, as -f check if it's a file:
-f FILE True if file exists and is a regular file
How to check/get file path relative to current script?
Script is running from ..../app/scripts/dev.sh
File to check from ..../app/dist/file.js
dir="${BASH_SOURCE%/*}../dist/backend.js"
if [ -f ${dir} ]; then
echo "file exists. ${dir}"
else
echo "file does not exist. ${dir}"
fi
There are three problems in your script.
To store the output of a command in a variable, use $(), not ${}.
[ -f "$dir" ] checks if $dir is a a file, which will never happen, because dirname outputs a directory.
Your script can be executed from any other working directory as well. Just because the script is stored in ยทยทยท/app/scripts/ does not mean it will always run from there.
Try
file=$(dirname "$BASH_SOURCE")/../dist/file.js
if [ -f "$file" ]; then
echo "file exists."
else
echo "file does not exist."
fi
I am attempting to write a bash script. In a test, I wrote a script to check for the existence of test.txt. However, no matter how many times I try to change the formatting, the code still does not recognize the file.
while [ "$INPUT" != "quit" ]; do
read INPUT
COMMANDFILE=test.txt
if [ -f $COMMANDFILE ]; then
echo "Found file!"
fi
done
I am 100% positive text.txt exists and is in the same folder as my script.
in this case I would add some debug lines to my script to make sure my thinking is correct. For instance:
while [ "$INPUT" != "quit" ]; do
read INPUT
COMMANDFILE=test.txt
echo "debug: now I'm in $( pwd ) directory. dir listing:"
ls -la
if [ -f $COMMANDFILE ]; then
echo "Found file!"
fi
done
also, please note that linux filenames are case sensitive (meaning you can have test.txt and TeSt.txt in the same directory). So, for instance if you have the file named TEST.TXT, [ -f test.txt ] will evaluate to false (unless test.txt exists as well)
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
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