Sponsored Content
Top Forums Shell Programming and Scripting Help turning pseudocode into ksh script Post 302858961 by Leedor on Wednesday 2nd of October 2013 05:23:18 AM
Old 10-02-2013
Thankyou!

Thanks so much RudiC, will try to test this shortly but after a bit of googling I think I have understood what you are trying to do, please let me know if I have misunderstood any commands. I changed the continue to a break as in case of any failure in verification, processing should stop.

Code:
# show commands as they are run (debug only)
set -vx
# Find HeaderFiles (HF) matching F57DWHDR*.csv
for HF in F57DWHDR*.csv
  # For each header file check 14 characters from 8th and ensure its >= 2013-01-01
  do    if [[ "${HF:8:14}" -ge 20130101000000 ]]
          # Check that every line in HF ends with * (search for any rows not ending in *)
          then  grep -qv \*$ $HF
                # Examine output of grep command
                case $? in
                  # Records were found in file not ending in *
                  (1)   echo FAIL2 $HF ;;
                  # File not found or syntax error
                  (2)   echo FAIL1 $HF ;;
                  # All records ended with *: loop through line by line reading filename (FN) and expected count (CNT), ignoring the REST
                  (0)   while read FN CNT REST
                          # check file exists and is 'readable'
                          do [ -r "$FN" ]                               \
                             # if not, fail and stop checking
                             ||    { echo FAIL3 $FN; break; }        \
                             # if readable, check line count of FN is equal to expected CNT
                             && [ "$(wc -l < $FN)" -eq $CNT ]           \
                                # if not equal, fail and stop checking
                                || { echo FAIL4 $FN; break; }        \
                                # if equal, show 'OK' and FN
                                &&   echo OK $FN
                          done < $HF ;;
                esac
          # header filename is not correctly formed
          else echo FAIL1 $HF
        fi
  done

---------- Post updated at 10:23 AM ---------- Previous update was at 08:25 AM ----------

An update ... have tested this a fair bit now and things are looking great, thanks again!

I had to take out the substring check for the date in the header filename as was getting 'bad subtitution' - from google perhaps I have an old version of ksh?

I switched the grep output cases (0) and (1) as 1 indicates no output (what we want).

One question: I realised my previous posted code with comments didn't work because it didn't have the \ to indicate its all still part of the same and/or ... putting \ on the end of all my comments didn't help either (guess they comment out the rest of the line) what's the best way to add comments to that section?

Thanks again, solution as it stands is below:

Code:
#! /usr/bin/ksh
# show commands as they are run (debug only)
#set -vx
# Check for HeaderFiles (HF) matching F57DWHDR*.CSV
if ls F57DWHDR*.CSV >/dev/null 2>&1; then
    echo Header file found
else
    echo No header files present
    exit 1
fi
for HF in F57DWHDR*.CSV
  # For each header file check it is readable
  do    if [[ -r $HF ]]
          # Check that every line in HF ends with * (search for any rows not ending in *)
          then  grep -qv \*$ $HF
                # Examine output of grep command
                case $? in
                  # Records were found in file not ending in *
                  (0)   echo $HF has records not ending in "*"
                        exit 1;;
                  # File not found or syntax error
                  (2)   echo $HF not found or grep syntax error
                        exit 1;;
                  # All records ended with *: loop through line by line reading filename (FN) and expected count (CNT), ignoring the REST
                  (1)   while read FN CNT REST
                          # check file exists and is 'readable'
                          do [ -r "$FN" ]                              \
                             ||    { echo $FN is not present and readable; exit 1; }                               \
                             && [ "$(wc -l < $FN)" -eq $CNT ]                                \
                                || { echo $FN record count does not match header $CNT ; exit 1; }                             \
                                &&   echo $FN has correct record count of $CNT
                          done < $HF ;;
                esac
          # header filename is not correctly formed
          else 
            echo $HF is not readable
            exit 1
        fi
  done
echo move files

 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Turning off the CDE

I am running Solaris 9 and wanted the CDE stopped when my users login. Can this be done by adding something to the .profile? Basically when they login they should be at the command line and have to start the CDE themselves. Thanks (11 Replies)
Discussion started by: meyersp
11 Replies

2. Shell Programming and Scripting

executing a ksh script from another ksh script

Hi, I'm new to unix scripting.How can i call a script from another script. I have a.ksh and b.ksh .I have to call b.ksh from a.ksh after it is successfully exceuted. I tried using #!/bin/ksh -x in a.ksh and at the end i have used /path/b.ksh My problem is it is executing only a.ksh.it... (6 Replies)
Discussion started by: ammu
6 Replies

3. UNIX for Dummies Questions & Answers

Turning Echo off

Hi, Is there any way like in dos to turn the echo off in a script? i have some lines popping up that i dont wish to be viewed when i am unziping a file it brings up the message updating: log.txt (deflated 72%) and extracting: log.txt i dont want these be viewed. Andy (4 Replies)
Discussion started by: chapmana
4 Replies

4. Shell Programming and Scripting

tracing a ksh script within a ksh script

I normally trace a script with the ksh -x <script name> and redirect strderr to file. But if you have a script like the examble below...... vi hairy bear=`grep bear animals` if then ksh more_animals fi If I ksh -x hairy it won't trace "more_animals" unless I put a -x in it. Is... (1 Reply)
Discussion started by: shorty
1 Replies

5. UNIX for Advanced & Expert Users

Algorithm In Pseudocode

A) produce an algorithm in pseudocode and a flowchart that gets n from the user and calculate their sum. B) Write an algorithm in pseudocode and a flowchart that gets number x from he user and calculates x5 ( X to the power of %5). Calculate by using multiplication. ... (1 Reply)
Discussion started by: delsega
1 Replies

6. Solaris

Turning in.ftpd on and off

For two straight days someone was running in.ftpd in my server (apparently looking to break in) and when I would do "top" almost every line would read "in.ftpd". I had a unix sysadmin friend of mine shut it down and then start it back up in a day and a half and all seems OK for now. Here's what I... (1 Reply)
Discussion started by: thomi39
1 Replies

7. Solaris

Script for turning processes in etc/inetd.conf on and off

Anyone have a perl script that can be run via a web browser to turn ftp or telnet on and off in etc/inetd.conf ? Believe it or not but I ride a motorcycle a lot in the summer and carry a laptop in my saddlebags to connect from almost anywhere via Verizon alongside the highway. However, have too... (0 Replies)
Discussion started by: thomi39
0 Replies

8. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies

9. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

10. Shell Programming and Scripting

Turning on Debugging for a perl script

for security reasons I can not post any part of the script in question in this thread. i hope im not breaking any rules by not doing so. but i have a perl script that i've been asked to turn on debugging on. i didn't write this perl script and i have very very little knowledge of perl. so i... (3 Replies)
Discussion started by: SkySmart
3 Replies
All times are GMT -4. The time now is 03:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy