1

So I have script xxxx.sh and I have additional options on that script. The question is when user runs this script I need check if the file exist or does not exist? example: ./xxxx.sh -f /examplefolder/anyfilename.Json for that i have this but idk if this will work?

# Validations # Does the file exist
if [[ ! -f $JSON_FILE ]]; then
echo "Error: The file $JSON_FILE does not exist."
exit 1
fi 
  • 4
    Did you try it? Do you have reason to believe it is *not* working? – chepner Aug 26 '22 at 16:57
  • 1
    It's not clear what the specific technical problem that led you to ask a question is. You're showing code, but not providing enough details to let someone reproduce a problem with that code in a way that's unambiguously the same problem you need our help with. If you don't know if you have a problem or not, it's too early to ask a question here; a Stack Overflow question needs to be about a narrow, specific technical problem _that you actually face_. – Charles Duffy Aug 26 '22 at 16:59
  • Well when i'm running command ./xxxx.sh -f /folder/folder/file.json It still states the file does not exist where in fact it does. Im i running the program wrong? –  Aug 26 '22 at 17:28
  • @chepner I tried it –  Aug 26 '22 at 17:35
  • What's the `-f` option in your script, where is it parsed? Where do you assign a value to the variable `JSON_FILE`? Is your working directory correct or are you using absolute paths? – knittl Aug 26 '22 at 19:33
  • @knittl i got project where user have to take json file and check if it exist . Because tool will work on everyones devices do i have specify a folder or Cant the user specify the folder? for instance (inside the script its that code above) ./xxx.sh -f /folder/myjsonfile.json –  Aug 26 '22 at 20:34
  • 1
    @Amateur_ik unfortunately, that didn't answer any of my questions. Where is `JSON_FILE` defined/assigned? How do you parse your command line? Note that you can run your script with `-x` to show exactly how each statement is executed: `sh -x xxx.sh -f /folder/myjsonfile.json` – hopefully this should give you an indication where (and why) your script is not working – knittl Aug 26 '22 at 20:36
  • @knittl so if i set the JSON_FILE = $3(positional parameter). Would that work? and how do i structure script better? –  Aug 26 '22 at 20:57
  • 1
    Your question is missing so much information. What will `echo "File=$JSON_FILE" show? Are you sure about the uppercase in `anyfilename.Json`? Does the filename have spaces? – Walter A Aug 26 '22 at 22:39

0 Answers0