Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Scripting a ksh script to read in a value and take action if that value is true.

Posted on 2004-10-11
3
Medium Priority
?
2,641 Views
Last Modified: 2013-11-17
I need a script that basically compares the value out of a text file and compares it to a value list.  If it meets the value, then it needs to replace the next value with the current value in the text file.  I have started the script as below, but I am having problems with it working right.  Please help.  Since I am fairly new at shell scripting, any suggestions would be greatly appreciated.  I do only prefer to use korn shell.

#!/bin/ksh
set -A array
array[1]="20041010"
array[2]="20041011"
array[3]="20041012"
array[4]="20041013"
array[5]="MH2004MAY"

if [ "${array[1]}" -eq "cut -c 81-89 gl265test" ] ; then
      sed 's/${array[1]},${array[2]}/'  gl265test > gl265.out
fi
if [ "${array[2]}" -eq "cut -c 81-89 gl265test" ] ; then
      sed 's/${array[2]},${array[3]}/'  gl265test > gl265.out
fi
if [ "${array[3]}" -eq "cut -c 81-89 gl265test" ] ; then
      sed 's/${array[3]},${array[4]}/'  gl265test > gl265.out
fi
if [ "${array[4]}" -eq "cut -c 81-89 gl265test" ] ; then
      sed 's/${array[4]},${array[5]}/'  gl265test > gl265.out
fi
if [ "${array[5]}" -eq "cut -c 81-89 gl265test" ] ; then
      sed 's/${array[5]},${array[1]}/'  gl265test > gl265.out
fi
0
Comment
Question by:cdeelena
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
tfewster earned 750 total points
ID: 12282727
The ","s in the `sed` command need to be "/"s, and the single quotes need to be double quotes, i.e.
sed "s/${search}/${replace}/"

The other potential issues I see are:
You're explicitly testing columns 81-89 for a match, but then the sed command will replace the _first_ matching pattern in the line;

The output, "> gl265.out"  means only the last match in the input file will appear in the output file; Did you mean to use ">>" ?
0
 
LVL 21

Expert Comment

by:tfewster
ID: 12282749
Oh, and the test  "-eq"  should only be used for integers; For string comparisons, use "="
0
 

Author Comment

by:cdeelena
ID: 12291672
Thanks tfewster,


The error in the sed command was my fault.  I knew it should be a /.  The single quotes did help out.  As far as the columns I am testing, the file is only a single line and the value I was testing for would only be found in column 81-89, so I was ok there.

I didn't know there was a difference for eq, so that helped as well.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question