Go Premium for a chance to win a PS4. Enter to Win

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,642 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

972 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