LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Solaris / OpenSolaris (https://www.linuxquestions.org/questions/solaris-opensolaris-20/)
-   -   create a directory at user home using shell script (https://www.linuxquestions.org/questions/solaris-opensolaris-20/create-a-directory-at-user-home-using-shell-script-804956/)

nagendrar 04-29-2010 07:35 AM

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

AlucardZero 04-29-2010 07:38 AM

try using $HOME instead of ~

catkin 04-29-2010 07:39 AM

What is $HOME set to when you have the problem?

jlliagre 04-29-2010 07:43 AM

You are using a shell that doesn't understand "~" like the old /bin/sh. Use ksh, bash or similar. AlucardZero workaround will work.

nagendrar 04-29-2010 08:10 AM

$HOME set to '/'

ThanQ,
Nagendra

jlliagre 04-29-2010 08:18 AM

Are you logged in as root ?

catkin 04-29-2010 08:24 AM

Quote:

Originally Posted by nagendrar (Post 3951791)
$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"?

nagendrar 04-30-2010 12:53 AM

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

ThanQ,
Nagendra

catkin 04-30-2010 01:15 AM

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?

nagendrar 04-30-2010 05:22 AM

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

jlliagre 04-30-2010 05:30 AM

Please clarify why do you want test to be created elsewhere than under / ?

What you show is correct and expected behavior.


All times are GMT -5. The time now is 08:58 AM.