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,636 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
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 20

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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 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…

860 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