Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to use a script to create a directory provided by user input. My test script is simple:
Code:
#!/bin/bash
echo "Type the path you want to create and push ENTER"
read userpath
mkdir -p $userpath
It creates the path just fine if I enter in normal names for directories, but if I try to use ~ to specify the user's home directory, it actually creates a directory in the current directory named "~".
That's a pain because I have to remove it by inode (something I didn't know how to do until a few minutes ago).
Is there a way to correctly process the ~ in a scripted folder creation?
Use the $HOME environment variable instead of the tilde.
I don't necessarily want it to go to home. I want the user to be able to enter any path (which probably should be home but who am I to judge )
I could just include instructions like "Please use /home/<username> instead of ~" but I want to idiot-proof this a bit more than that for people that are even less familiar with Linux than I am.
#!/bin/bash
echo "Type the path you want to create and push ENTER"
read userpath
if [[ $userpath == ~* ]]
then mkdir -p $HOME/${userpath:1};
else
mkdir -p $userpath;
fi
#!/bin/bash
MINPARAMS=1
if [ -n "$1" ]
then
mkdir -pv "$1"
fi
if [ $# -lt "$MINPARAMS" ]
then
echo "`basename $0` $HOME/some/directory"
echo "that you wish to create"
fi
exit 0
the
echo "Please type a valid $HOME/some/directory"
is merely a visual suggestion to the user and not enforced.
I used mkdir -pv to offer feedback via the script as to the result.
example:
Code:
./mymkdir.sh LQRocks
mkdir: created directory LQRocks
#!/bin/bash
echo "Type the path you want to create and push ENTER"
read userpath
if [[ $userpath == ~* ]]
then mkdir -p $HOME/${userpath:1};
else
mkdir -p $userpath;
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.