my shell script (file modifications)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting my shell script (file modifications)
# 1  
Old 03-24-2008
my shell script (file modifications)

hi guys

Need some help on my below script

#!/bin/sh
if [ -f /root/joy/inittab ]
then
echo "~~:S:wait:/sbin/sulogin" >> /root/joy/inittab
else
echo "/root/joy/inittab does not exist"
fi

now the problem is that when i run the above script it runs successfully
but when i run it repeatedly the word echo "~~:S:wait:/sbin/sulogin" gets added to the file irrespectively the no of times i run the script.
I jst want the line created jst once in the file irrespective of times i run the script.How do i do pls help and thanks in advance

reg:ash
# 2  
Old 03-24-2008
Hope this helps:

#!/bin/bash

if [ -f /root/joy/inittab ]
then
# check to see if inittab already contains this line
grep "~~:S:wait:/sbin/sulogin" /root/joy/inittab >/dev/null

# check return code from grep 0 line was found 1 or higher it wasn't
errorcode=`echo $?`
if [ $errorcode -ne 0 ]
then
echo "~~:S:wait:/sbin/sulogin" >> /root/joy/inittab
fi
else
echo "inittab does not exist"
fi
# 3  
Old 03-24-2008
Just as a stylistic comment, "if" looks at the error code from whatever command you run it on; "if test $?" is a rather roundabout way of saying it.

Code:
#!/bin/bash

if [ -f /root/joy/inittab ]
then
  # check to see if inittab already contains this line
  if ! grep "~~:S:wait:/sbin/sulogin" /root/joy/inittab >/dev/null
  then
    echo "~~:S:wait:/sbin/sulogin" >> /root/joy/inittab
  fi
else
  echo "inittab does not exist"
fi

In theory, there is a race condition here; you should take care not to end up running many of these at the same time, or they will stumble over each other.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Shell Scrip in Masking particular columns in .csv file or .txt file using shell script

Hello Unix Shell Script Experts, I have a script that would mask the columns in .csv file or .txt file. First the script will untar the .zip files from Archive folder and processes into work folder and finally pushes the masked .csv files into Feed folder. Two parameters are passed ... (5 Replies)
Discussion started by: Mahesh G
5 Replies

2. Shell Programming and Scripting

Modifications using Lookup file

I have 2 files. File 1 delimited by ";" File 1: 001;0;11223;xx;N;1001051;124;1;1;1001051;110;0;A_15;D;DX;U 001;0;8830943;xx;P;1226040;978;1;0;1226040;110;0;A_15;D;DX;H 001;0;10946903;xx;N;1300496;978;1;1;1300496;110;0;A_17;D;DX;H... (2 Replies)
Discussion started by: pparthiv
2 Replies

3. Shell Programming and Scripting

script to find whether the difference between two files in only additions but not modifications

i basically have 2 files and sdiff of the files is given below sdiff file1 file 2 control_file < path INDEX < size 613 < mode 0444 ... (1 Reply)
Discussion started by: under_cons
1 Replies

4. Shell Programming and Scripting

Modifications to a file

I have a file whose format is shown below. It has a table of numbers. In this case, I have 16 values in 12 rows. I want to select a position in the table, example, the 5th number at row 5. I need to change the value in that position by a certain amount and output the file with the modifiation. ... (6 Replies)
Discussion started by: kristinu
6 Replies

5. Shell Programming and Scripting

Modifications to a file

Hi, I do not have a clue how to do this nor can I find information on it but I have a file that looks like this (basically 3 columns and tab delimited). I need this in a particular format in order for a program to actually read it. chr1 2 4 chr1 2 5 chr1 3 6 chr2 1 4 chr2 2 5 ... (2 Replies)
Discussion started by: kylle345
2 Replies

6. Shell Programming and Scripting

XML file modifications using sed

Hi, During an installation process in a bash script I need to do 2 things with 2 XML files. Does the use of sed affect in any way the XML file ? 1.Add to a section in <ServerListeners> section <ServerListener> <BaseClass>myapp.module.WowConfigurator</BaseClass> </ServerListener> The... (2 Replies)
Discussion started by: potro
2 Replies

7. Shell Programming and Scripting

how to write modifications in to two tables

hi, how to write modifictions of two tables in the ksh (0 Replies)
Discussion started by: naveeng.81
0 Replies

8. Shell Programming and Scripting

question about testing in shell programming(modifications were made)

In folder A i have a file "a' and text file named infile00.I would like to do redirection :a<infile01. There is a code to do this #get a file "a" in /home/A for file in /home/A/* do if $file ] then #printing out if file is an execute file echo $file "is an... (2 Replies)
Discussion started by: thungmail
2 Replies

9. UNIX for Advanced & Expert Users

Tracing file modifications

Hello all! Is there a way or a utility to trace any kind of file changes in a particular directory on any UNIX machine? The purpose is that in Unix, there are multiple ways of opening and making changes to a file. But internally, there must be something common (a single pipe, etc.) that is... (3 Replies)
Discussion started by: gupta_ca
3 Replies

10. Shell Programming and Scripting

In Line File Modifications: Search and Replace

grep -il "TEST" ${ENVIRON}/*.pde| while read pde &nbsp;&nbsp;do &nbsp;&nbsp;&nbsp;&nbsp;cat $pde | sed s/"TEST 3,1"/"TEST 3,0"/g | sed s/"TEST&nbsp;&nbsp;3,1"/"TEST&nbsp;&nbsp;3,0"/g > ${pde}.tmp &nbsp;&nbsp;&nbsp;&nbsp;if ; then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mv ${pde}.tmp $pde ... (2 Replies)
Discussion started by: Shakey21
2 Replies
Login or Register to Ask a Question