Executing a command within `if` statement and on success perform further steps
Solution 1
The echo always succeeds. Do without it and the subshell:
#!/bin/bash
echo "enter the directory name"
read ab
check(){
if mkdir "$ab" 2>/dev/null; then
echo "directory created "
ls -ld "$ab"
exit
else
echo "try again "
echo "enter new value for directory: "
read ab
check
fi
}
check
Solution 2
(( echo `mkdir $ab` 2>/dev/null )) bash: ((: echo 2>/dev/null : syntax error in expression (error token is "2>/dev/null ")
Double parentheses surround arithmetic expressions. That command that doesn't do anything close to what you intended. mkdir $ab
doesn't write anything to standard output, so `mkdir $ab`
is an empty string, so the content of the arithmetic expression is echo 2>/dev/null
. The >
sign is the “greater than” operator here, and echo
is a variable name. The arithmetic expression is syntactically incorrect (you can't have a variable followed by a literal integer). Since the command is syntactically incorrect, it returns an error status, and the else
branch is taken.
To test the return status of a command, just write that command.
if mkdir -- "$ab" 2>/dev/null
Additional tips:
- It's a bad idea to hide the errors from a command. The user needs to know if the error is permission denied, I/O error, etc. To create a directory if it isn't there and succeed if the directory already exists, use
mkdir -p -- "$ab"
. - Always use double quotes around variable substitutions.
- If the value of
ab
begins with a dash, it'll be interpreted as an option. Put--
first to mark the end of options.
Solution 3
tries=0
while [ "$((tries+=1))" -le 5 ] || ! break
printf 'enter dirname: > '
IFS= read -r dir &&
if ! mkdir -- "$dir"
then echo try again
else ! echo Success\!
fi
do :; done
I'm not sure why you want to negate the exit statuses like that, but I think the above should get something like the inversion you're looking for - and it should quit after a limited number of $tries
.
If I run it...?
enter dirname: > .
mkdir: cannot create directory ‘.’: File exists
try again
enter dirname: > .
mkdir: cannot create directory ‘.’: File exists
try again
enter dirname: > .
mkdir: cannot create directory ‘.’: File exists
try again
enter dirname: > .
mkdir: cannot create directory ‘.’: File exists
try again
enter dirname: > .
mkdir: cannot create directory ‘.’: File exists
try again
And... again...
enter dirname: > doesntexist
Success!
Related videos on Youtube
![AshwaraS](https://lh6.googleusercontent.com/-Jfc67w70Nlc/AAAAAAAAAAI/AAAAAAAAAE4/83ufH3FLW8A/photo.jpg?sz=256)
AshwaraS
Updated on September 18, 2022Comments
-
AshwaraS almost 2 years
I was trying to execute a program which will create a directory on the basis of a complete path provided to it from the prompt and, if the directory already exists it will return an error (for "directory already exists") and ask again for the name in a recursive function.
Here it is what I tried: let us say a file,
test1
in it:#!/bin/bash echo "enter the directory name" read ab check(){ if (( echo `mkdir $ab` 2>/dev/null )); then echo "directory created " echo `ls -ld $ab` exit else echo "try again " echo "enter new value for directory:" read ab check fi } check
The problem here is if the directory exists then the program works fine but if it does not exist then it creates it but then goes to the else part of the program.
-
mikeserv over 9 yearsIn fact there is a whole proper command construct called
case
for this - all of those layeredif
s might be confusing.