LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 12:22 PM   #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, 12:46 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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 12:48 PM.
 
Old 09-19-2014, 01:18 PM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,788
Blog Entries: 1

Rep: Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341
Use the $HOME environment variable instead of the tilde.
 
Old 09-19-2014, 01: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 01:27 PM.
 
Old 09-19-2014, 01:35 PM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,788
Blog Entries: 1

Rep: Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341
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 01:37 PM.
 
Old 09-19-2014, 01:36 PM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,121

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
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, 02:10 PM   #7
netnix99
Member
 
Registered: Jun 2011
Distribution: redhat, CentOS, OpenBSD
Posts: 298

Rep: Reputation: 98
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, 02: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 02:14 PM.
 
Old 09-19-2014, 02:16 PM   #9
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,168
Blog Entries: 10

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
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 02:20 PM.
 
Old 09-19-2014, 02: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, 02:17 PM   #11
netnix99
Member
 
Registered: Jun 2011
Distribution: redhat, CentOS, OpenBSD
Posts: 298

Rep: Reputation: 98
Glad to help!!
 
Old 09-20-2014, 05:37 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,121

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 09:40 AM
.recycle folder creation & use gemmajid Linux - General 5 09-08-2012 10:36 AM
Shell Scripting text file creation issue AiresTheBold Linux - Newbie 1 02-16-2011 10:27 AM
[SOLVED] Find folder creation time kenneho Linux - Newbie 2 08-24-2009 08:56 AM
Scripting the creation of a DVD DriverJC Programming 1 03-08-2006 06:58 PM


All times are GMT -5. The time now is 12:30 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration