Two Scripting Questions

Two Scripting Questions

Post by Multi-User-P » Fri, 27 Jul 2001 04:47:41



Hi,

First question:

In a simple practice script I have the following line:

mkdir $1/public_html

I want that line to create a directory called public_html under the user's
default home.

As it stands, (as written above), the command line would have to be "sh
nameofscript ~username".

I want to be able to do away with the tilde "~" on the command line.

How can I write the script so that the "~" is put in by the script?

In other words, the command line would be "sh nameofscript username".

The script would then insert the "~" and the system would know that it means
the default home directory.

I tried the obvious: "mkdir ~$1/public_html, as well as some variations, but
no go.

****************************************************************************
*****

Second question:

If I log in as say, user1, and do a command line like this:

echo $HOME

I get the path to my default home directory.

How can I, logged in as say root, ask the system "What is user22's default
home directory"?

Or, "What is user22's $HOME"?

****************************************************************************
******

Thanks,

 
 
 

Two Scripting Questions

Post by M.J. Bl » Fri, 27 Jul 2001 05:15:25



> Hi,

> First question:

> In a simple practice script I have the following line:

> mkdir $1/public_html

> I want that line to create a directory called public_html under the user's
> default home.
> As it stands, (as written above), the command line would have to be "sh
> nameofscript ~username".
> I want to be able to do away with the tilde "~" on the command line.

mkdir /home/$1/public_html

Quote:> Second question:

> If I log in as say, user1, and do a command line like this:

> echo $HOME

> I get the path to my default home directory.

> How can I, logged in as say root, ask the system "What is user22's default
> home directory"?

> Or, "What is user22's $HOME"?

Look in the /etc/passwd. The home directories are there. Or use awk
:-)

$ awk -F":" '/menno/ {print $6}' /etc/passwd

(replace "menno" with the username you'd like to check)

HTH

--
Menno

 
 
 

Two Scripting Questions

Post by Chris Coyl » Fri, 27 Jul 2001 06:12:24




> > Hi,

> > First question:

> > In a simple practice script I have the following line:

> > mkdir $1/public_html

> > I want that line to create a directory called public_html under the user's
> > default home.
> > As it stands, (as written above), the command line would have to be "sh
> > nameofscript ~username".
> > I want to be able to do away with the tilde "~" on the command line.

> mkdir /home/$1/public_html

Except that you are assuming that user's home dir is /home/user
(which it usually is, but not necessarily).  You have given op a way to
find user's home dir in /etc/passwd (below), which could be used here as

    mkdir $(awk -F":" user=$1 '/user/ {print $6}' /etc/passwd)/public_html

or, much simpler just to go there:

    (cd; mkdir public_html)

Putting it in parentheses makes it run in a new shell, so you don't have
to worry about losing your current context.

- Show quoted text -

Quote:> > Second question:

> > If I log in as say, user1, and do a command line like this:

> > echo $HOME

> > I get the path to my default home directory.

> > How can I, logged in as say root, ask the system "What is user22's default
> > home directory"?

> > Or, "What is user22's $HOME"?

> Look in the /etc/passwd. The home directories are there. Or use awk
> :-)

> $ awk -F":" '/menno/ {print $6}' /etc/passwd

> (replace "menno" with the username you'd like to check)

> HTH

> --
> Menno

 
 
 

Two Scripting Questions

Post by M.J. Bl » Fri, 27 Jul 2001 07:13:22





[...]
>> > I want that line to create a directory called public_html under the user's
>> > default home.

>> mkdir /home/$1/public_html

> Except that you are assuming that user's home dir is /home/user
> (which it usually is, but not necessarily).  You have given op a way to
> find user's home dir in /etc/passwd (below), which could be used here as

>     mkdir $(awk -F":" user=$1 '/user/ {print $6}' /etc/passwd)/public_html

> or, much simpler just to go there:

>     (cd; mkdir public_html)

> Putting it in parentheses makes it run in a new shell, so you don't have
> to worry about losing your current context.

True again, but I still not see why you would want such a script
after all ;-) If you only want to create one directory for one
single user at a time, you'd be better off doing:

# mkdir ~user/public_html

If you want all new users to have this directory, do a:

# mkdir /etc/skel/public_html

If you want to create a directory for every know user, just go to
the default home directory (e.g. /home/) and do:

# ls | while read USER; do mkdir $USER/public_html;\
  chown $USER:group $USER/public_html"; done

That is, if you only have user directories (no other files or
directories) in that /home :-)

You could also get the usernames and homedirectories from the
/etc/passwd, but please note the daemon accounts!

--
Menno

 
 
 

Two Scripting Questions

Post by Elf Sternbe » Sat, 28 Jul 2001 02:38:23




Quote:>First question:
>In a simple practice script I have the following line:
>mkdir $1/public_html

>I want that line to create a directory called public_html under the user's
>default home.

#!/bin/bash
USER=$1
HOME=`grep "^$USER\:" /etc/passwd | cut -d':' -f6`
if [ "X$HOME" != "X" ]; then
        mkdir -p $HOME/public_html      
else
        echo "ERROR Did not find user $USER on system"
fi

Quote:>How can I, logged in as say root, ask the system "What is user22's default
>home directory"?

#!/bin/bash
USER=$1
HOME=`grep "^$USER\:" /etc/passwd | cut -d':' -f6`
if [ "X$HOME" != "X" ]; then
        echo "$HOME"
else
        echo "ERROR Did not find user $USER on system"
fi

Other variants of the HOME line include:

HOME=`gawk -F':' "\\$1 ~ /^$USER\\$/ { print \\$6 }" /etc/passwd`

HOME=`perl -e "print ((getpwnam(\"$USER\"))[7])"`

HOME=`finger $USER | grep 'Directory' | cut -d' ' -f2`

And my favorite, just because it's SO ugly (and yes, you need both parts
of it):

SED="/^$USER/"'{
        s/^[^:]*:[^:]*:[^:]*:[^:]*:[^:]*://
        s/:.*$//
        p

Quote:}'

HOME=`sed -ne "$SED" /etc/passwd`

                Elf

--
Elf M. Sternberg, Immanentizing the Eschaton since 1988
http://www.halcyon.com/elf/

Today is gone, today was fun.  Tomorrow will be a better one.
From there to here, from here to there, funny things are everywhere.

 
 
 

Two Scripting Questions

Post by Glitc » Sat, 28 Jul 2001 05:00:31


Quote:> True again, but I still not see why you would want such a script
> after all ;-) If you only want to create one directory for one
> single user at a time, you'd be better off doing:

> # mkdir ~user/public_html

> If you want all new users to have this directory, do a:

> # mkdir /etc/skel/public_html

> If you want to create a directory for every know user, just go to
> the default home directory (e.g. /home/) and do:

> # ls | while read USER; do mkdir $USER/public_html;\
>   chown $USER:group $USER/public_html"; done

> That is, if you only have user directories (no other files or
> directories) in that /home :-)

> You could also get the usernames and homedirectories from the
> /etc/passwd, but please note the daemon accounts!

the daemon acccounts could be skipped by using a test condition that
only reads lines with the UID, $3, being higher than 499.  The first
user created always has a UID of 500.
 
 
 

Two Scripting Questions

Post by M.J. Bl » Sat, 28 Jul 2001 05:06:16




[...]
>> You could also get the usernames and homedirectories from the
>> /etc/passwd, but please note the daemon accounts!

> the daemon acccounts could be skipped by using a test condition that
> only reads lines with the UID, $3, being higher than 499.  The first
> user created always has a UID of 500.

That's not a certain thing. On normal systems, yes, but I could
create a user account with a lower UID, if I really wanted to.

--
Menno

 
 
 

Two Scripting Questions

Post by Multi-User-P » Sat, 28 Jul 2001 05:22:17


To answer the above question about why such a script is needed:

I only posted a portion of the script. Actually the script will have several
functions, only one of which is to create the directory mentioned.

It will:

1) Add a user based on the name given at the command line
2) Create a sub directory called "public_html"
3) Chmod the users home directory
4) Chmod the "public_html" directory
5) Move some files to the "public_html" directory
6) Chmod the files moved to that directory

So, as you can see, a script would make things much easier than running all
of these commands each time a new user is added.







> [...]
> >> > I want that line to create a directory called public_html under the
user's
> >> > default home.

> >> mkdir /home/$1/public_html

> > Except that you are assuming that user's home dir is /home/user
> > (which it usually is, but not necessarily).  You have given op a way to
> > find user's home dir in /etc/passwd (below), which could be used here as

> >     mkdir $(awk -F":" user=$1 '/user/ {print $6}'

/etc/passwd)/public_html
Quote:

> > or, much simpler just to go there:

> >     (cd; mkdir public_html)

> > Putting it in parentheses makes it run in a new shell, so you don't have
> > to worry about losing your current context.

> True again, but I still not see why you would want such a script
> after all ;-) If you only want to create one directory for one
> single user at a time, you'd be better off doing:

> # mkdir ~user/public_html

> If you want all new users to have this directory, do a:

> # mkdir /etc/skel/public_html

> If you want to create a directory for every know user, just go to
> the default home directory (e.g. /home/) and do:

> # ls | while read USER; do mkdir $USER/public_html;\
>   chown $USER:group $USER/public_html"; done

> That is, if you only have user directories (no other files or
> directories) in that /home :-)

> You could also get the usernames and homedirectories from the
> /etc/passwd, but please note the daemon accounts!

> --
> Menno

 
 
 

Two Scripting Questions

Post by Multi-User-P » Sat, 28 Jul 2001 05:22:38


To answer the above question about why such a script is needed:

I only posted a portion of the script. Actually the script will have several
functions, only one of which is to create the directory mentioned.

It will:

1) Add a user based on the name given at the command line
2) Create a sub directory called "public_html"
3) Chmod the users home directory
4) Chmod the "public_html" directory
5) Move some files to the "public_html" directory
6) Chmod the files moved to that directory

So, as you can see, a script would make things much easier than running all
of these commands each time a new user is added.

Quote:> Hi,

> First question:

> In a simple practice script I have the following line:

> mkdir $1/public_html

> I want that line to create a directory called public_html under the user's
> default home.

> As it stands, (as written above), the command line would have to be "sh
> nameofscript ~username".

> I want to be able to do away with the tilde "~" on the command line.

> How can I write the script so that the "~" is put in by the script?

> In other words, the command line would be "sh nameofscript username".

> The script would then insert the "~" and the system would know that it
means
> the default home directory.

> I tried the obvious: "mkdir ~$1/public_html, as well as some variations,
but
> no go.

****************************************************************************

- Show quoted text -

Quote:> *****

> Second question:

> If I log in as say, user1, and do a command line like this:

> echo $HOME

> I get the path to my default home directory.

> How can I, logged in as say root, ask the system "What is user22's default
> home directory"?

> Or, "What is user22's $HOME"?

****************************************************************************

- Show quoted text -

Quote:> ******

> Thanks,

 
 
 

Two Scripting Questions

Post by Multi-User-P » Sat, 28 Jul 2001 05:26:58


Thanks!

I thank you all for the answers.

 
 
 

Two Scripting Questions

Post by M.J. Bl » Sat, 28 Jul 2001 18:25:11



> To answer the above question about why such a script is needed:

> I only posted a portion of the script. Actually the script will have several
> functions, only one of which is to create the directory mentioned.

> It will:

> 1) Add a user based on the name given at the command line
> 2) Create a sub directory called "public_html"
> 3) Chmod the users home directory
> 4) Chmod the "public_html" directory
> 5) Move some files to the "public_html" directory
> 6) Chmod the files moved to that directory

> So, as you can see, a script would make things much easier than running all
> of these commands each time a new user is added.

This really sounds like you want to check out the /etc/skel
directory :-) All files from that directory will be created in the
new user's $HOME and, IIRC, chmod'ed automatically.

HTH

--
Menno

 
 
 

Two Scripting Questions

Post by Frank Ranne » Mon, 30 Jul 2001 22:01:47



> Hi,

> First question:

> In a simple practice script I have the following line:

> mkdir $1/public_html

> I want that line to create a directory called public_html under the user's
> default home.

> As it stands, (as written above), the command line would have to be "sh
> nameofscript ~username".

> I want to be able to do away with the tilde "~" on the command line.

> How can I write the script so that the "~" is put in by the script?

> In other words, the command line would be "sh nameofscript username".

> The script would then insert the "~" and the system would know that it means
> the default home directory.

> I tried the obvious: "mkdir ~$1/public_html, as well as some variations, but
> no go.

eval mkdir ~$1/public_html

Quote:

> Second question:

> If I log in as say, user1, and do a command line like this:

> echo $HOME

> I get the path to my default home directory.

> How can I, logged in as say root, ask the system "What is user22's default
> home directory"?

> Or, "What is user22's $HOME"?

1. echo ~user22
or
2. getent passwd user22 | awk -F: '{print $6}'
or
3. getent passwd user22 | cut -d: -f6

to set a variable to some users home directory enclose one of the above
in backticks

uhome=`getent passwd user22 | cut -d: -f6`

try the following in a script:

#!/bin/bash
uhome=`eval echo ~$1`
echo "result is " $uhome

------------
regards,
Frank Ranner

 
 
 

Two Scripting Questions

Post by Invalid&quo » Sat, 04 Aug 2001 01:41:02



> Hi,

> First question:

> In a simple practice script I have the following line:

> mkdir $1/public_html

> I want that line to create a directory called public_html under the user's
> default home.

> As it stands, (as written above), the command line would have to be "sh
> nameofscript ~username".

> I want to be able to do away with the tilde "~" on the command line.

> How can I write the script so that the "~" is put in by the script?

> In other words, the command line would be "sh nameofscript username".

> The script would then insert the "~" and the system would know that it means
> the default home directory.

> I tried the obvious: "mkdir ~$1/public_html, as well as some variations, but
> no go.

> ****************************************************************************
> *****

> Second question:

> How can I, logged in as say root, ask the system "What is user22's default
> home directory"?

> Or, "What is user22's $HOME"?

I'd use the follow (for both points):

#!/bin/sh
home=`grep $1 /etc/passwd | awk -F":" '{print $6}'`
mkdir $home/public_html

As far as I know, you can't expand ~ to *someone else's* home.
(I may be wrong though...)

Lee.

 
 
 

1. Two easy scripting questions

Question 1: How can I wait for input for N seconds, then default to
something if nothing is read?

Question 2: Given a full path to a filename, what's an easy way to
extract the name of the directory containing that file, and assign
it to an environment variable?

Thanks
Moshe

--

Geek code v3.12 (www.geekcode.com):
GCS/E d- s+:-- a-- C++$ UL++>+++$ P+>++ L+++>$ E--- W+ N++ w--
!O M-- V? PS+ PE Y+ !PGP t 5? X+ R- tv b- DI+ D+ G e>++ h r y

2. sun workshop c compiler warning

3. two questions about Autoconf scripts

4. APC Smart UPS 1400RM's DB9 Port?

5. Two SUID-ROOT SCRIPT questions

6. Linux on A Compaq DeskPro/Prof. Station

7. 2 second question - 2 second answer

8. DSL under Mandrake 7.0

9. Newbie connect question: Two PCs, both RH 5.1, two NE2000 cards: failure to ping!!!!!!!!!!!

10. How to setup two terminals, two keyboards, two mouses?

11. two routers (two wans) connecting to one lan using two nics.

12. two dsl connections, two routers, dual nics on linux box , want to run two websites

13. A Solaris 8 (Bourne Shell) Scripting question, regarding a log rotation script