Can someone verify the code of two function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can someone verify the code of two function
# 1  
Old 10-15-2011
Can someone verify the code of two function

Code:
Input=D123,S1234,D345 | kESTREL PRODUCTION SUPPORT

echo -en "Enter the logmsg="
read logmsg
logmsg1=${logmsg%%|*};
echo "$logmsg1"|tr ',' '\n' | sed 's/[ \t]*$ | sed '/^$/d'//'>pre-commit.config

Char()
{
while read line
do
if [[ ${line:0:1 = D ]] || [[ ${line:0:1 = S ]];then
echo "Success"
else
exit 1;
done<pre-commit.config
}

length()
{
while read LINE
do
len=`expr length "$LINE"`
if [ "$LINE" -le "6" ];then
echo "Success"
else
exit 2;
# and using the variable len for some purpose
done < pre-commit.config
}

length
Char


In the above script I want the two things

1) Each variable in the left of pipe should start with D and S
2) Each variable is of 6 characters
# 2  
Old 10-15-2011
Quote:
Originally Posted by rohit22hamirpur
Code:
Input=D123,S1234,D345 | kESTREL PRODUCTION SUPPORT

echo -en "Enter the logmsg="
read logmsg
logmsg1=${logmsg%%|*};
echo "$logmsg1"|tr ',' '\n' | sed 's/[ \t]*$ | sed '/^$/d'//'>pre-commit.config

Char()
{
while read line
do
if [[ ${line:0:1 = D ]] || [[ ${line:0:1 = S ]];then
echo "Success"
else
exit 1;


You are missing the closing fi
Quote:
Code:
done<pre-commit.config
}

length()
{
while read LINE
do
len=`expr length "$LINE"`


Since you are using bash or ksh, use parameter expansion for the length:
Code:
len=${#LINE}

Quote:
Code:
if [ "$LINE" -le "6" ];then


You are comparing $LINE istead of $len.
Quote:
Code:
echo "Success"
else
exit 2;


Again, you are missing the closing fi
Quote:
Code:
# and using the variable len for some purpose
done < pre-commit.config
}

length
Char


In the above script I want the two things

1) Each variable in the left of pipe should start with D and S
2) Each variable is of 6 characters


I'd write the functions like this:
Code:
input='D123,S1234,D345 | kESTREL PRODUCTION SUPPORT'

Char()
{
  for var
  do
    case $var in
      [DS]*) echo Success ;;
      *) exit 1 ;;
    esac
  done
}

length()
{
  for var
  do
    if [ ${#var} -le 6 ]
    then
      echo Success
    else
      exit 2
    fi
  done
}

input=${input%% | *}

IFS=, read -a vars <<< "$input"

length "${vars[@]}"
Char "${vars[@]}"

This User Gave Thanks to cfajohnson For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Returning an exit code from a bash function

How to return a exit code from a function and use it in conditional? I tried the following but it does not seem to work. tests.sh: if test ./load.sh ; then echo "0" else echo "1" fi load.sh: return 1; from command line: $ ./tests.sh 0 I was expecting it to output "1"... (17 Replies)
Discussion started by: wolfv
17 Replies

2. Homework & Coursework Questions

I need help movingworking code into library function and calling it obj13-2.pl

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! I need help moving working code into library function called obj13-lib.pl and call the same function in obj13-2.pl I am a Linux newbie and this certificate is my first step... (0 Replies)
Discussion started by: cllinuxhelp
0 Replies

3. Shell Programming and Scripting

Error using function in AWK code

Hi to all, I have an AWK code that looks like this: awk 'NR==FNR { #code to process file1 next } { #code to process file2 a=myfunction(x) } END { #Some code b=myfunction(x) ... (3 Replies)
Discussion started by: Ophiuchus
3 Replies

4. UNIX for Dummies Questions & Answers

Verify a flat file with UNIX function or script.

I re-post my question to seek your help and critique. I was required to verify a very large and tab-delimited file, named 'MyFile'. 1. The each line in 'MyFile' has 7 columns, and that the values in the 5th column are integers. I need to use shell functions (and standard LINUX/UNIX filters) to... (1 Reply)
Discussion started by: duke0001
1 Replies

5. UNIX for Dummies Questions & Answers

Verify the data type in a file with UNIX function

I am seeking help on this UNIX function, please help. Thanks in advance. I have a large file, named as 'MyFile'. It was tab-delmited, I am told that each record in column 1 is unique. How would I verify this with UNIX function or command? (1 Reply)
Discussion started by: duke0001
1 Replies

6. Shell Programming and Scripting

Verify large file with Unix function

I am seeking help on one UNIX function writting. Please help. I have a large file, named 'MyFile', It was tab-delmited. I am told that each line in 'MyFile' has 7 columns, and that the values in the 5th column are integers. How do I use shell functions (and standard LINUX/UNIX filters) to verify... (1 Reply)
Discussion started by: duke0001
1 Replies

7. Shell Programming and Scripting

My menu function [new code -revised]

Below is my menu options code. It allows user to pick one of two option. I can get the first option to work, because if it is selected it just "breaks" and carries on with the script. What i want to know is if a user selects option two how do i get that option to ignore all the other script and... (3 Replies)
Discussion started by: amatuer_lee_3
3 Replies

8. Programming

how to write a wrapper c code to return uid using getuid() function

And how to use setuid() ? thanks (4 Replies)
Discussion started by: pwd
4 Replies

9. Shell Programming and Scripting

Please verify the simple Shell code

Hi, i am trying to write script which will delete files(more than 90 days older) from different directories. Please check its ok if i implement it in SUN solaris10; One important thing i used here "exec" flag. I heard from someone- as i have lots of files "exec" flag may give error like too... (2 Replies)
Discussion started by: thepurple
2 Replies

10. Shell Programming and Scripting

PHP function kills my code?

So I'm making a page for one of my web sites that scans the visitors ports (just a few specific ones, not ALL) and tells them if they have any open. Now I made up the code and it worked great. Now instead of having the same chunk of code all over my script, I tried to make my own subroutine so I... (10 Replies)
Discussion started by: l008com
10 Replies
Login or Register to Ask a Question