LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris and OpenSolaris.
General Sun, SunOS and Sparc related questions also go here.

Notices

Reply
 
Search this Thread
Old 04-29-2010, 07:35 AM   #1
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 141

Rep: Reputation: 15
create a directory at user home using shell script


I am trying for creating a "test" directory in user home using shell script as follows

mkdir -p ~/test

But it creates ~/test directory at script executing path instead of creating "test" directory in user home . But when i run "mkdir -p ~/test" on terminal then it creates test directory successfully in user home .

Please help me to create directory in user home.

ThanQ,
Nagendra
 
Old 04-29-2010, 07:38 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,610

Rep: Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518
try using $HOME instead of ~
 
Old 04-29-2010, 07:39 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
What is $HOME set to when you have the problem?
 
Old 04-29-2010, 07:43 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,486

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
You are using a shell that doesn't understand "~" like the old /bin/sh. Use ksh, bash or similar. AlucardZero workaround will work.
 
Old 04-29-2010, 08:10 AM   #5
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 141

Original Poster
Rep: Reputation: 15
$HOME set to '/'

ThanQ,
Nagendra
 
Old 04-29-2010, 08:18 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,486

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Are you logged in as root ?
 
Old 04-29-2010, 08:24 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by nagendrar View Post
$HOME set to '/'

ThanQ,
Nagendra
$HOME should be set to the user's home directory, the same as output by the following command (with root replaced by the relevant user name)
Code:
grep '^root:' /etc/passwd | cut -d':' -f6
Alternatively, following jlliagre's line of thought, which shell are you using? echo $SHELL should tell you but is not 100% reliable.

EDIT: what was "script executing path"?

Last edited by catkin; 04-29-2010 at 08:26 AM. Reason: emboldening
 
Old 04-30-2010, 12:53 AM   #8
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 141

Original Poster
Rep: Reputation: 15
This " grep '^root:' /etc/passwd | cut -d':' -f6 " command gives "/" .This is smae as $HOME. Here I am logged in as root .

ThanQ,
Nagendra
 
Old 04-30-2010, 01:15 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
OK, so you are logged in as root, root's home directory is / and $HOME is correctly set. We still don't know which shell root is using so please post the output of
Code:
grep '^root:' /etc/passwd | cut -d':' -f7
Going back to your OP

Quote:
mkdir -p ~/test

But it creates ~/test directory at script executing path instead of creating "test" directory in user home . But when i run "mkdir -p ~/test" on terminal then it creates test directory successfully in user home.
How had you got to the command prompt when you ran the command the first time? Were you using a terminal emulator in the graphical environment?

Please post a copy-and-paste of doing that so we can see exactly what happened, along with "extras". Something like this.
Code:
c@CW8:/tmp$ echo $HOME
/home/c
c@CW8:/tmp$ echo $SHELL
/bin/bash
c@CW8:/tmp$ pwd
/tmp
c@CW8:/tmp$ mkdir -p ~/test
c@CW8:/tmp$ ls -ld ~/test
drwxr-xr-x 2 c users 1 2010-04-30 11:39 /home/c/test
When you wrote "But when i run "mkdir -p ~/test" on terminal" what do you mean by "terminal"? Do you mean one of the virtual terminals, normally accessed via Ctrl+Alt+F<1 to 6>? If you can do so, please post a copy-and-paste of doing the same as above at your "terminal" command prompt. If you can't do that, please post what is different.

When your wrote "But when i run "mkdir -p ~/test" on terminal then it creates test directory successfully in user home", do you mean directory /test was created?

Last edited by catkin; 04-30-2010 at 01:16 AM. Reason: typoanarchy
 
Old 04-30-2010, 05:22 AM   #10
nagendrar
Member
 
Registered: Apr 2008
Location: HYD, INDIA.
Posts: 141

Original Poster
Rep: Reputation: 15
Output of " grep '^root:' /etc/passwd | cut -d':' -f7" is /sbin/sh

Quote:
How had you got to the command prompt when you ran the command the first time?
Were you using a terminal emulator in the graphical environment?
Connect the machine using ssh from my Linux machine terminal command line.

Quote:
Please post a copy-and-paste of doing that so we can see exactly what happened, along with "extras". Something like this
Code:
      
bash-3.00# echo $SHELL
/sbin/sh
bash-3.00# echo $HOME
/
bash-3.00# mkdir ~/test
bash-3.00# pwd
/tmp
bash-3.00# ls -ld ~/test/
drwxr-xr-x   2 root     root         512 Apr 30 06:17 //test/
bash-3.00#
Quote:
When your wrote "But when i run "mkdir -p ~/test" on terminal then it creates test directory successfully in user home", do you mean directory /test was created?
Yes, test was created in '/'

ThanQ,
Nagendra
 
Old 04-30-2010, 05:30 AM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,486

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Please clarify why do you want test to be created elsewhere than under / ?

What you show is correct and expected behavior.
 
  


Reply


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
Help with bash script to create directory to /home folder wegadnie Linux - General 5 10-25-2008 11:50 AM
create a new user with a different home directory cccc Debian 6 08-06-2008 05:47 AM
Auto Create user home directory on Samba m2azer Linux - Networking 2 01-15-2007 11:56 AM
create ftp user for vsftpd server without home directory cccc Linux - Networking 2 07-30-2005 06:32 AM
adding a new user doesn't create a home directory abrakadabra Linux - General 6 08-09-2002 08:11 PM


All times are GMT -5. The time now is 04:14 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration