LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 09-19-2014, 11:22 AM   #1
bulldoggk
LQ Newbie
 
Registered: Apr 2014
Posts: 7

Rep: Reputation: Disabled
Help scripting folder creation


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?

Thanks!
 
Old 09-19-2014, 11:46 AM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by bulldoggk View Post
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).
There's no need for that, just delimit it
Code:
$ ls -l
total 0
$ mkdir "~"
$ ls -l
total 4
drwxrwxr-x 2 user user 4096 Sep 19 10:45 ~
$ rmdir \~
$ ls -l
total 0
Now files/directories that begin with a "-", THOSE are fun to try and get rid of.

I'm afraid I can't help with your actual question though...

Last edited by suicidaleggroll; 09-19-2014 at 11:48 AM.
 
Old 09-19-2014, 12:18 PM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Use the $HOME environment variable instead of the tilde.
 
Old 09-19-2014, 12:25 PM   #4
bulldoggk
LQ Newbie
 
Registered: Apr 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
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.

Last edited by bulldoggk; 09-19-2014 at 12:27 PM.
 
Old 09-19-2014, 12:35 PM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Oh. Use 'eval' to do the tilde substitution then.

ie:

Code:
[root@dev ~]# DIR=~/whatever
[root@dev ~]# eval 'echo $DIR'
/root/whatever
Is that what you mean?

Last edited by szboardstretcher; 09-19-2014 at 12:37 PM.
 
Old 09-19-2014, 12:36 PM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
you can try:
Code:
#!/bin/bash

echo "Type the path you want to create and push ENTER"
read userpath
userpath=$(echo $userpath)

....
 
1 members found this post helpful.
Old 09-19-2014, 01:10 PM   #7
netnix99
Member
 
Registered: Jun 2011
Distribution: redhat, CentOS, OpenBSD
Posts: 298

Rep: Reputation: 99
bulldoggk,

Is this what you are looking for???

Code:
#!/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
 
Old 09-19-2014, 01:11 PM   #8
bulldoggk
LQ Newbie
 
Registered: Apr 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
Oh. Use 'eval' to do the tilde substitution then.

ie:

Code:
[root@dev ~]# DIR=~/whatever
[root@dev ~]# eval 'echo $DIR'
/root/whatever
Is that what you mean?
That looked promising, but it didn't work for me.

Script:

Code:
#!/bin/bash

echo "Type the path you want to create and push ENTER"
read USERPATH
echo $USERPATH
eval 'echo $USERPATH'
Output:

Code:
gabe@ubuntu:~$ ./test
Type the path you want to create and push ENTER
~/test1/maybe/this/
~/test1/maybe/this/
~/test1/maybe/this/
Does it matter that I'm on Ubuntu 13.10? @Pan64 - your suggestion had the same result (but thanks!)

Last edited by bulldoggk; 09-19-2014 at 01:14 PM.
 
Old 09-19-2014, 01:16 PM   #9
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Real basic:
Code:
#!/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’
verification:
Code:
ls -ld LQRocks/
drwxr-xr-x 2 jj2 jj2 4096 Sep 19 2014  2:17 PM LQRocks/

Have fun with it.

Last edited by Habitual; 09-19-2014 at 01:20 PM.
 
Old 09-19-2014, 01:16 PM   #10
bulldoggk
LQ Newbie
 
Registered: Apr 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by netnix99 View Post

Code:
#!/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
Perfect, thanks!
 
Old 09-19-2014, 01:17 PM   #11
netnix99
Member
 
Registered: Jun 2011
Distribution: redhat, CentOS, OpenBSD
Posts: 298

Rep: Reputation: 99
Glad to help!!
 
Old 09-20-2014, 04:37 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by bulldoggk View Post
That looked promising, but it didn't work for me.

Script:

Code:
#!/bin/bash

echo "Type the path you want to create and push ENTER"
read USERPATH
echo $USERPATH
eval 'echo $USERPATH'
Output:

Code:
gabe@ubuntu:~$ ./test
Type the path you want to create and push ENTER
~/test1/maybe/this/
~/test1/maybe/this/
~/test1/maybe/this/
Does it matter that I'm on Ubuntu 13.10? @Pan64 - your suggestion had the same result (but thanks!)
you must not use eval. It should work, but I need to see the real script you tried...
 
  


Reply

Tags
bash scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting and log file redirecting and creation. Learin Linux - Newbie 14 05-24-2014 08:40 AM
.recycle folder creation & use gemmajid Linux - General 5 09-08-2012 09:36 AM
Shell Scripting text file creation issue AiresTheBold Linux - Newbie 1 02-16-2011 09:27 AM
[SOLVED] Find folder creation time kenneho Linux - Newbie 2 08-24-2009 07:56 AM
Scripting the creation of a DVD DriverJC Programming 1 03-08-2006 05:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration