Question in bash script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question in bash script.
# 1  
Old 07-30-2013
Question in bash script.

Hi All,
I need an assistance with the issue below.
I wrote big script in "bash" that automatically install an LDAP on Clients.
I'd be happy to know in order to avoid duplication of entries in files,
How i can define into the script, if the specific expressions already exist in the file, do not add them again.
For example i take the following section from the script :
Code:
echo "admin ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
echo "%linuxadmin ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

How i define in the script , only if the value are already exists in the file , do not add it again.
i have the same thing with sections that write expressions to more files.
The thing is that i sometimes i need to run the script more than once on the same server,and as a result of that it created duplicate values ​​in multiple files.
Thank you guys,

Aviel.

Last edited by Scott; 07-30-2013 at 05:12 AM.. Reason: Removed excessive formatting; added code tags
# 2  
Old 07-30-2013
If you just want to avoid duplicating the string you would add with your script...
Code:
egrep "$value" $file || echo "$value" >> $file

# 3  
Old 07-30-2013
Skrynesaver,
can you be more specific,

At the section :
Code:
#Add permission to sysadmin
echo "admin ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
echo "%linuxadmin ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

Where i should put the expressions before or after this section ,
And how do you write the expression you gave me that would be suitable for the pare here,

Thanks ,

Aviel.

Last edited by Scott; 07-30-2013 at 05:13 AM.. Reason: Removed formatting; added code tags
# 4  
Old 07-30-2013
It's just checking the return value of grep for the existence of the record you wish to add in the file...
Code:
value='admin ALL=(ALL) NOPASSWD: ALL'
file=/etc/sudoers
grep "$value" $file || echo "$value" >> $file
value="%linuxadmin ALL=(ALL) NOPASSWD: ALL"
grep "$value" $file || echo "$value" >> $file

# 5  
Old 07-30-2013
I Add it to the script, but it didnt work.
The Duplicate value still getting added to /etc/sudoers
# 6  
Old 07-30-2013
Quote:
Originally Posted by Aviel.shani
I Add it to the script, but it didnt work.
The Duplicate value still getting added to /etc/sudoers
Did you replace the 2 lines echoing the value to the file without checks?
# 7  
Old 07-30-2013
Added to the script :

Code:
value='admin ALL=(ALL) NOPASSWD: ALL'
file=/etc/sudoers
grep "$value" $file || echo "$value" >> $file
value="%linuxadmin ALL=(ALL) NOPASSWD: ALL"
grep "$value" $file || echo "$value" >> $file

Code:
deleted from script :

Code:
#Provide to system administrators accounts root privileges
echo "admin        ALL=(ALL)       NOPASSWD: ALL"   >> /etc/sudoers echo "%linuxadmin  ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers


Last edited by Scott; 07-30-2013 at 06:22 AM.. Reason: CODE tags please... not bold tags, not font tags, not colour tags... CODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Script Iterating Question

I am trying to look through one of my directories to remove certain files. I am pretty new to Unix and bash so I just need a little help in starting this. I know I would have to write two loops one to iterate the directories and one to iterate the files. How would I write the loops? (3 Replies)
Discussion started by: totoro125
3 Replies

2. Shell Programming and Scripting

[BASH] Performance question - Script to STDOUT

Hello Coders Some time ago i was asking about python and bash performances, and i was told i could post the regarding code, and someone would kindly help to make it faster (if possible). If you have noted, i'm on the way to finalize, finish, stable TUI - Text(ual) User Interface. It is a... (6 Replies)
Discussion started by: sea
6 Replies

3. Shell Programming and Scripting

Question about Shebang line of Bash Script

Hello All, I was writing a Bash shell script that will be executed on both an AIX server (/usr/bin/ksh) and a SLES server (/bin/bash). The AIX server has Bash installed at "/usr/bin/bash", which is in a different dir then the SLES server. So basically I am writing the script on the SLES... (4 Replies)
Discussion started by: mrm5102
4 Replies

4. Shell Programming and Scripting

Question about writing a bash script

Hello, I want to write a bash script to delete the content after '#'. However, if '#' appears in a string with "", ignore this. For example, input file: test #delete "test #not delete" Output file: test "test #not delete" Does anyone know how to write this script? Thanks (1 Reply)
Discussion started by: jeffwang66
1 Replies

5. Shell Programming and Scripting

Bash script - loop question

Hi Folks, I have a loop that goes through an array and the output is funky. sample: array=( 19.239.211.30 ) for i in "${array}" do echo $i iperf -c $i -P 10 -x CSV -f b -t 50 | awk 'END{print '$i',$6}' >> $file done Output: 19.239.211.30 19.2390.2110.3 8746886 seems that when... (2 Replies)
Discussion started by: nitrohuffer2001
2 Replies

6. Solaris

bash shell send script question

Hi Experts, Need your help. I am trying to send a command via ssh to about a hundred network devices. I intend to do this via a bash script something similar to the below: ssh -l user testmachine.com "show version" Obviously this will not work given the password prompt that comes... (2 Replies)
Discussion started by: marcusbrutus
2 Replies

7. Shell Programming and Scripting

bash script question

Can anybody be kind to explaing me what the lines below mean ? eval line=$line export $line ] || echo $line (2 Replies)
Discussion started by: jville
2 Replies

8. Shell Programming and Scripting

BASH script question

Hi, I want to create a script that gets a filename as an argument. The script should generate a listing in long list format of the current directory, sorted by file size. This list must be written to a new file by the filename given on the command line. Can someone help me with this? ... (6 Replies)
Discussion started by: I-1
6 Replies

9. Shell Programming and Scripting

one question for bash shell script

Just one question for bash shell script. In bash script, you can use *.txt to call any files in current folder that ends with .txt, like rm *.txt will remove all txt file in current folder. My question is can you actually remember or use the file name among *.txt, I know file=*.txt will not... (9 Replies)
Discussion started by: zx1106
9 Replies

10. Shell Programming and Scripting

BASH shell script question

I want to make shell script that takes a list of host names on my network as command line arguments and displays whether the hosts are up or down, using the ping command to display the status of a host and a for loop to process all the host names. Im new to shell scripting so Im not quite sure... (3 Replies)
Discussion started by: ewarmour
3 Replies
Login or Register to Ask a Question