Home > database >  Stdout: User file name input
Stdout: User file name input

Time:11-28

Im writing a data formating bash script. The script reads the file name which is input by the user in the terminal. Of course, when no file under that name is found in that directory, the program ends with a stderr output. I am now trying to implement a (while) loop which recursively asks for user input until a matching file is found and then goes on to executing my data formating commands. Would appreciate some help :)

CodePudding user response:

I am now trying to implement a (while) loop which recursively

I would advice against using recursion for this. Just make a simple loop. It could look like this:

while true; do
    IFS= read -rp 'File: ' file
    if [[ -e $file ]]; then
        break;
    fi
    echo "$file doesn't exist, try again"
done

# work with $file here
  • Related