1

I am a little new to bash scripting and I was trying to make this program capture a directory path from user and then check if the directory is present. It seems as if the variable SRC inside the dir_input function is not passed to dir_input. Thanks in advance.

dir_input() {
    echo -n "Enter targer directory: "
    read SRC
}
dir_input_check() {
    if [ ! -d "$SRC" ]; then
        echo ERROR: "Target directory not found - Please try again."
        dir_input
    fi
}

dir_input
dir_input_check
3
  • How exaxtly do you run the script? May 14, 2015 at 1:05
  • Re "Seems like the variable SRC inside the dir_input function does not passes to dir_input." The calue in SRC does not come from the variablr in dir_input. It's direct nre input. May 14, 2015 at 1:10
  • It seems to work for ma, as far as I see - can you show a test that shows something gors wrong? May 14, 2015 at 1:12

1 Answer 1

0

It seems to work for me. Adding a

#!/bin/bash -x

at the top to get a trace, I see this (if I enter /tmp in reply):

> ./foo
+ dir_input
+ echo -n 'Enter targer directory: '
Enter targer directory: + read SRC
/tmp
+ dir_input_check
+ '[' '!' -d /tmp ']'

So SRC is passed as expected to dir_input_check. Perhaps there is something left out of the example.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .