ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Redhat Linux 8 & 9, Fedora Core 2, XP
Posts: 301
Rep:
Simple script for adding groups to Linux
Howdy!
I am trying to write a script for adding groups and users for my samba domain... Here is what I have tried writing, I have not started on the user part since I cannot get the groups to work properly:
Code:
#!bin/bash
echo -n "Enter group name: "
read group_name
#checks to see if group is already added to /etc/group
if [ "$group_name" == "grep '$group_name' /etc/group" ]; then
echo "Group already exists! Skipping group creation..."
exit 1
else
/usr/sbin/groupadd $group_name
echo "Group added"
fi
Output:
# sh grouptest.sh
Enter group name: test
Group added
#sh grouptest.sh
Enter group name: test
groupadd: group test exists
Group added
Seem to me the the portion just below the if statement is entirely ignored... Why is that?
I have programming basics, just not good at it thats all
It seems that your IF statement is always wrong even if the group already exists
For example, if you type:
Code:
grep root /etc/group
root:x:0:
the result is the whole line with hidden password and GID... so to correct it either you use a "|cut" extension to have only group name or you find another way like:
Code:
if [$(grep $group_name $group_file | wc -l) -eq 1]
Yes, you're right keefaz But it was only an example... a wrong one
But the solution with the "cut" command is good as you tell the script to keep the first word of the line
Distribution: Redhat Linux 8 & 9, Fedora Core 2, XP
Posts: 301
Original Poster
Rep:
Quote:
We are false. Try with a group named "roo" so I revisit my example
Code:
group_test=$(cat /etc/group | awk -F: '{ print $1 }' | grep $group_name)
if [ -z $group_test ] || [ ! "$group_test" = "$group_name" ]; then
-- add the group
else
-- print group exist
fi
OK... let me confirm a few things:
- The commands inside $(...|...|...) greps the first column (before the first colon) of /etc/group which matches the entered group_name, right? then assigns the value to group_test...
- then the first if condition checks if group_test returns a null, right?
More questions on expressions...
- $(...|...|...), whats the use of the $ and | sign?
- for anything within "...", the value stored in that variable is compared?
- whats the difference if I use [ "$group_test" != "$group_name" ] instead?
- also difference between = and ==
There is no difference between /usr/sbin/groupadd $group_name and /usr/sbin/groupadd "$group_name". But when in test case the "" are needed to extrapolate the value from the variable.
For the question whats the use of the $ and | sign?, the $ sign means the result will be put in a variable and the | is a pipe symbol. It is a useful command line concept. It take the result of the previous command (before the | ) and work with it.
Example :
echo "Hello" | wc -c
echo "Hello" # print Hello
| wc -c # take Hello and display its number of char
You can use
[ "$group_test" != "$group_name" ]
or [ ! "$group_test" = "$group_name" ]
it is the same.
From the man bash :
"= may be used in place of == for strict POSIX compliance"
so you can use '=' or '==' for testing strings
I am not a bash expert but your prob interested me also I doubt that the script will be usefull as the command groupadd will prevent you if a group already exist.
For the integer between permission and ownership in ls -l, do ls -a in a directory, count the directory files, then cd.. and do ls -l, you should see the same number in the second field of output ls -l ( between permission and ownership ).
I think your $group_name variable is not initialised (no value) so It can't create directory.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.