Solved

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

Posted on 2004-10-11
3
2,632 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
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
tfewster earned 250 total points
Comment Utility
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 20

Expert Comment

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

Author Comment

by:cdeelena
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now