LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 02-04-2018, 11:41 AM   #1
snakes3177
LQ Newbie
 
Registered: Feb 2018
Posts: 9

Rep: Reputation: Disabled
Help for a linux -Debian script


allo

Last edited by snakes3177; 02-18-2018 at 09:42 AM.
 
Old 02-04-2018, 01:40 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,763

Rep: Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225
What happens when you run that script? What errors are you getting?

Here's one hint:
Code:
#! / Bin / bash
should be
Code:
#!/bin/bash
to define the location of bash and run the script with the bash shell.

Another hint:
If you define a variable as FILE, then you must reference it as $FILE, not '$ file'. Case always matters!

Clean up your script and let us know what happens.
 
1 members found this post helpful.
Old 02-04-2018, 01:55 PM   #3
snakes3177
LQ Newbie
 
Registered: Feb 2018
Posts: 9

Original Poster
Rep: Reputation: Disabled
allo

Last edited by snakes3177; 02-18-2018 at 09:47 AM.
 
Old 02-04-2018, 03:59 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,276
Blog Entries: 24

Rep: Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224
Welcome to LQ!

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.
 
1 members found this post helpful.
Old 02-04-2018, 04:08 PM   #5
snakes3177
LQ Newbie
 
Registered: Feb 2018
Posts: 9

Original Poster
Rep: Reputation: Disabled
allo

Last edited by snakes3177; 02-18-2018 at 04:07 PM.
 
Old 02-04-2018, 04:50 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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.

read Substring Removal
http://tldp.org/LDP/abs/html/string-manipulation.html

Last edited by BW-userx; 02-04-2018 at 05:01 PM.
 
2 members found this post helpful.
Old 02-04-2018, 04:53 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,276
Blog Entries: 24

Rep: Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224Reputation: 4224
Quote:
Originally Posted by snakes3177 View Post
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.
 
Old 02-04-2018, 05:11 PM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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
 
1 members found this post helpful.
Old 02-04-2018, 05:12 PM   #9
snakes3177
LQ Newbie
 
Registered: Feb 2018
Posts: 9

Original Poster
Rep: Reputation: Disabled
allo

Last edited by snakes3177; 02-18-2018 at 04:07 PM.
 
Old 02-04-2018, 05:14 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by snakes3177 View Post
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.
Code:
userx@slackwhere:~
$ var1=100
 
$ echo $var1
100
 
$ var1=$((var1+=1))
 
$ echo $var1
101

Last edited by BW-userx; 02-04-2018 at 05:19 PM.
 
1 members found this post helpful.
Old 02-04-2018, 05:21 PM   #11
snakes3177
LQ Newbie
 
Registered: Feb 2018
Posts: 9

Original Poster
Rep: Reputation: Disabled
allo

Last edited by snakes3177; 02-18-2018 at 04:08 PM.
 
Old 02-04-2018, 05:28 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
btw
Code:
GROUP=input

if [[ $(getent group $GROUP /etc/group) ]]
then
	echo "Group exists"
else
	echo "groupadd $GROUP -g $GID"
fi
that too works. I just tested it.

Last edited by BW-userx; 02-04-2018 at 05:30 PM.
 
1 members found this post helpful.
Old 02-04-2018, 05:32 PM   #13
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Use the second line from my signature. I mean the bash -x. Not about asking.

jlinkels
 
Old 02-04-2018, 05:56 PM   #14
snakes3177
LQ Newbie
 
Registered: Feb 2018
Posts: 9

Original Poster
Rep: Reputation: Disabled
allo

Last edited by snakes3177; 02-18-2018 at 09:48 AM.
 
Old 02-04-2018, 06:13 PM   #15
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Echo != echo
 
  


Closed Thread



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Debian Bash Script Help Please... ilovepoker Linux - Newbie 1 10-02-2014 07:30 PM
Migrating mail server from debian 5 to debian 6- Maildir folder renaming script asylum_craig Linux - Server 1 04-05-2013 06:46 AM
debian 6.0.4 init script pini Debian 1 03-10-2012 06:19 AM
Shell script to compile a custom Linux kernel on Debian-based systems Kenny_Strawn Linux - Kernel 3 09-25-2010 01:29 PM
Equivalent pw script in Debian or any Linux? lunajlt Linux - General 2 12-21-2005 03:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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