LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-04-2006, 09:59 PM   #1
CrimsonSkyZS
LQ Newbie
 
Registered: Mar 2005
Location: USA
Distribution: Fedora Core 4
Posts: 12

Rep: Reputation: 0
Question Using Variables in a Command that uses a Directory (Shell Script)


Hi, I am writing a simple script to make and setup a user account for me. I'm sure you can get the general jist of the script by reading it. I am trying to create a directory with the read in username variable. How do I note the variable so it is substituted for its value?

Code:
#!/bin/bash

echo -n 'Username: '
read username
echo -n 'Full Name: '
read fullname

useradd -d /home/"$USERNAME" -g users -k /etc/skelcfiles "$USERNAME"
chfn -f "$FULLNAME" "$USERNAME"
chown -R "$USERNAME" /home/"$USERNAME"
chgrp -R users /home/"$USERNAME"
chmod -R go-rwx /home/"$USERNAME"
I've tried the folling notation:
$USERNAME
"$USERNAME"
"echo $USERNAME"
'$USERNAME'
${USERNAME}

On a side note, syntax highlighting in vim only works like half the time. I also want to enable syntax highlighting for php in vim.

Last edited by CrimsonSkyZS; 01-04-2006 at 10:01 PM.
 
Old 01-04-2006, 10:09 PM   #2
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
I'm not very good with bash. Just from a quick browse of your code I noticed that you named the variable "username" but you are trying to access it via "$USERNAME". I believe variables are case sensitive. Give "$username" a shot (minus the quotes). I hope this helps!
 
Old 01-04-2006, 11:47 PM   #3
CrimsonSkyZS
LQ Newbie
 
Registered: Mar 2005
Location: USA
Distribution: Fedora Core 4
Posts: 12

Original Poster
Rep: Reputation: 0
Code:
[root@redhost ~]# ./unixa
Username: username
Full Name: Full Name
./unixa: line 8: /home/username: No such file or directory
./unixa: line 8: username: command not found
useradd: invalid home directory `-s'
./unixa: line 9: Full: command not found
./unixa: line 9: username: command not found
chfn: option `--f' requires an argument
Usage: chfn [ -f full-name ] [ -o office ] [ -p office-phone ]
        [ -h home-phone ] [ --help ] [ --version ]
./unixa: line 10: username: command not found
./unixa: line 10: /home/username: No such file or directory
chown: too few arguments
Try `chown --help' for more information.
./unixa: line 11: /home/username: No such file or directory
chmod: too few arguments
Try `chmod --help' for more information.
I've fixed it up a bit, don't know whats going on now...
I found this "Expanded is a very appropriate word: the shell basically sees the string $USER and replaces it with the variable's value then executes the command."
But when I use double quotes it doesn't seem to work.
Code:
#!/bin/bash

echo -n 'Username: '
read USERNAME
echo -n 'Full Name: '
read FULLNAME

useradd -d `/home/$USERNAME` -s /bin/bash -k /etc/skelcfiles -g users `$USERNAME`
chfn --f `$FULLNAME` `$USERNAME`
chown -R `$USERNAME`:users `/home/$USERNAME`
chmod -R go-rwx `/home/$USERNAME`

Last edited by CrimsonSkyZS; 01-04-2006 at 11:50 PM.
 
Old 01-05-2006, 04:31 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Unfortunately, if you have $USER defined to something, "$USERNAME" may be interpreted as "$USER"NAME (ie
the value of $USER, followed by "NAME".
To avoid this, use curly braces {} (see man bash):
ie ${USERNAME}
This makes it 100% clear to the shell that you want to use the value of the variable "USERNAME".

Centinul also has an interesting point. Variable names are indeed case sensitive.

Also, for clarity, include the ENTIRE string in "" when there's a variable that needs to be expanded inside it.
So use "/home/${USERNAME}" not /home/"$USERNAME" or something.

When you want to use the output string of a command like echo, you need to include it in backquotes: ``.
Double quotes won't do the trick.
"echo $myVar" is nothing more than a string "echo <value of myVar is put here>".
`echo $myVar` will echo the value of $myVar.
ie a=`echo $myVar|sed -e 's/foo/bar/'` echoes $myVar's value, replaces "foo" with "bar" and stores the result
in $a.

Finally, note that useradd takes care of the "chown" and "chgrp" for you. When you use "useradd" to add a user, it will automatically create the user's home dir, using appropriate owner/group.
The "-R" option of chmod is unnecessary. Blocking r/x permission on the home directory to everyone but the
owner will effectively prevent everyone (except the user in question) from accessing anything under that home directory.
But I'm wondering if useradd also doesn't do that for you (depends on config, probably).
Use useradd's -c option to specify the user's "full name" in the password file. chfn does something different.
 
Old 01-11-2006, 03:18 PM   #5
CrimsonSkyZS
LQ Newbie
 
Registered: Mar 2005
Location: USA
Distribution: Fedora Core 4
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks for the tips, greatly appreciated. Helped a lot.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help setting environment variables via shell script srosburg Linux - Newbie 2 12-08-2005 07:58 PM
Setting environment variables from shell script theta Linux - General 5 09-02-2004 08:50 PM
Specifying target directory for command in bash shell script? spectrescape Programming 1 07-22-2004 05:37 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM
Why?? can not use variables with shell script Bassam Linux - General 9 01-27-2004 07:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:22 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