Forums

variable in batch naming folders

OK I'm new to this...curious why I can't create folders via script...

15:18 ~$ cat myscript

/bin/bash

clear
echo "Enter a type of fruit:"
read fruit
mkdir /home/pistolaNola/fruit/$fruit
touch /home/pistolaNola/fruit/$fruit/$fruit
echo "The file and directory $fruit have been created in /home/pistolaNola/fruit."

./myscript

Enter a type of fruit:
apple
mkdir: cannot create directory ‘/home/pistolaNola/fruit/apple’: No such file or directory
touch: cannot touch ‘/home/pistolaNola/fruit/apple/apple’: No such file or directory
The file and directory apple have been created in /home/pistolaNola/fruit.

thanks in advance for any advice offered! I'm working on learning UNIX and ditching windows altogether

does the directory "fruit" exist? you can use "mkdir -p" to create all the intermediate directories needed, if you like...

the script is supposed to take the 'read fruit' and mkdir with the $fruit variable...the training I'm using uses solaris so might just be symantics across dif versions of linux/unix

the $fruit variable appears to be the issue, if I just create the dir with a folder it works, so maybe this just isnt possable in bash in python anywhere?

I like using pythonanywhere since its free and really easy to use, but i guess not being able to get to root etc may have some limitations...

No, I mean, do you actually already have a directory called "/home/pistolaNola/fruit"? you can't make a folder at ./fruit/apple, if ./fruit doesn't already exist. unless you use -p

ah..thank you sir!