Results 1 to 3 of 3
I need to make a script that does exactly:
1. Create a new user. Ask for the username, full name, and password. Then use this information to create a new ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 11-01-2012 #1Just Joined!
- Join Date
- Nov 2012
- Posts
- 1
Urgent help needed with a shell script (Creating new user and backup)
I need to make a script that does exactly:
1. Create a new user. Ask for the username, full name, and password. Then use this information to create a new user in the system.
2. Backup an entire partition. Ask for the partition name (directory associated with that partition) and the target location to save the backup file. Then make a copy of that directory in the desired location as a single archive file, with current date as the file extension.
4. Change to a different runlevel. Ask the new runlevel number (0-6) from the user and change to that runlevel with appropriate commands.
This is what I have:
This isnt a practical script, so I dont need to worry about errors or making sense. I just need to make a simple script that does the things listed above.Code:case $input in 1) echo "Enter Username:"; read user echo "Enter Full Name:"; read name sudo useradd -c $name $user sudo passwd $user echo "User added" sleep 5 ;; 2) echo "Enter partition address:"; read partAddress echo "Enter destination address:"; read reqAddress tar -cf "$reqAddress-$(date+'%Y%m%d').tar" "${partAddress}" echo "Done" sleep 10 ;; 4) echo "Enter new run level:" read level sudo init $level echo "Done"Last edited by Mephistopheles; 11-01-2012 at 01:58 AM.
- 11-02-2012 #2Just Joined!
- Join Date
- Jul 2008
- Posts
- 90
Please do your own homework. Don't ask here for someone to do it.
- 11-02-2012 #3Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,657
Hello and welcome!
As clowenstein has mentioned, asking homeworking question is against Forum Rules.
However, we don't mind giving tips if you need them, provided that you show some effort, and I think you have. So here are some tips:
1. Is this just a snippet of your code, or is this all of it? I.e., is $input defined somehow? Otherwise, the case will never get run.
2. you can use the --stdin argument with passwd, when run as root, like this:
3. your date command needs a space before the plus sign, e.g.:Code:echo "mypassword"|passwd --stdin $username
4. do you really mean to back up a partition, or do you mean a directory? if you do mean a partition, you need to do some more work: namely, you need to determine the partition upon which the directory given by the user exists. For this, see the df command.Code:$(date +'%Y%m%d')
5. when changing run levels (assuming you are on a SysV Init system), you ought to use telinit, not init.
6. when you run your useradd command, you may want to enclose the variable containing the full name in double-quotes, in case spaces were used (i.e., useradd -c "$fullname" $username)
7. you are missing the closing double semicolons in your last case statement. you can only leave off the double semicolons in the *) case (I think).
other than that, you code is workable. I would change the way the user gives the input (i.e., the read parts). Basically, I would just stuff them in loops to make sure the user enters something before hitting Enter.


Reply With Quote
