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.
Remember that the shell as most things in the Linux environment is case sensitive - Bin != bin, Echo != echo.
Also, please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.
do you expect this to work?
it is what would be called pseudo code.
Code:
#verify if the group exists
if getent group $GROUP/etc/group
Then
echo "Group exists"
as stated BASH is case sensitive for one. meaning no caps .. your if statement is malformed, Then.. you need to watch out for spacing.
Code:
userx@slackwhere:~
$ GROUP=users
userx@slackwhere:~ // no space here gets no return.
$ getent group $GROUP/etc/group
userx@slackwhere:~ // add space between group variable and data file being sought for a match or not.
$ getent group $GROUP /etc/group
users:x:100:
if you look at your output, even this will not work, you need to look into string malnipultaion
Code:
#!/bin/bash
GROUP=users
#verify if the group exists
if [[ $GROUP == $(getent group $GROUP /etc/group) ]] ;
then
echo "Group exists"
else
# making this an echo statement gives you the ability to see what is
#happening without it actually taking place.
echo "groupadd $GROUP -g $GID"
fi
you are needing to compare your source to the output of what actually is there, or not. intake of group name is only a name of the group and not the formatting of the group file. So you need to make adjustments to your code to get a proper result from your test.
removing the right side of the returned string up to and including the : farthest to the left of the string, in order to just have your group name left to compare it to what you are checking for.
Code:
#!/bin/bash
GROUP=users
GOT=$(getent group $GROUP /etc/group)
GOT=${GOT%%:*}
echo $GOT
echo
#verify if the group exists
#can be written like this using the above
#string already cut to where it needs to be
if [[ $GROUP == $GOT ]] ;
then
echo "Group exists"
else
echo "groupadd $GROUP -g $GID"
fi
#or cut it within the if statement
if [[ $GROUP == ${GOT%%:*} ]]
then
echo "Group exists"
else
echo "groupadd $GROUP -g $GID"
fi
therefore, if GOT comes up blank then your if statement executes the command after the else.
i'm not sure to understand what this mean "please place your code snippets inside
Code:
...
tags for better readability."
i never done linux coding before
Quote:
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.
That is not about Linux coding, it is about your posts here on LQ. Please read that again and follow the link in "tags" for more complete information. That link is also available at the bottom of most pages here on LQ in the BB code link.
Using proper tag markup in your posts preserves indentation and visually sets code apart from your comments making it much easier to read.
furthermore, as I removed a bit of your code to point out that one part, then went back and looked at your code again, here, your logic here in your actual code. I put it to question.
Code:
#verify if the group exists
if getent group $GROUP/etc/group
Then
echo "Group exists"
#if your test fails no group match achieved, then this next line will never be executed.
#because it is within the if test returns true. If group is not present then it returns false.
#so where is the logic? it will never reach this code to give you the GID you need for the
#creation of your group that does not exist.
#2.Create the group if it does not exist. The gid is retrieved from the Group column in the account.txt file (column3)
Gid=$(Cut -f3 account.txt)
else
groupadd $GROUP -g $GID
fi
can you explin this section, i never saw that before
GOT=${GOT%%:*}
echo $GOT
echo
I gave you the link and the heading to look for that explains what you are now asking me in the that same post.
this part here is just reassigning the variables value within the variable of the same name. it saves having to come up with another variable name to use.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.