Induct two functions in a single function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Induct two functions in a single function
# 1  
Old 10-14-2011
Induct two functions in a single function

Code:
echo -en "Enter the logmsg="  {D1234,S1234 | rohit singh}
read logmsg
logmsg1=${logmsg%%|*};
echo $logmsg1 |sed "s/$/,/g"|sed 's/[ \t]*$//'>pre-commit.config
logmsg_len2=$(echo ${logmsg} | awk '{print $2}');
logmsg_suffix="|";
 
length()
{
cat "pre-commit.config"| awk -F'[,|]' '{for(i=1;i<NF;i++) { if (length($i) <= 6) { print " success" } else { print " Defect|Story number should not be more than 6 Characters i.e. D12345 or S12345";exit 1;}}}';
}
 
Char()
{
echo $logmsg1 | tr ',' '\n' | awk '{if($0~/^D/||/^S/) {print " "} else {print " Defect|Story number should be started with D or S";exit 1;}}'
}


The above script is working fine but that is causing issue so I want can soneone help me how we can induct these two functions in single.


Thanks-
# 2  
Old 10-14-2011
What shell are you using?

You say the script is working fine but it seems to me that there is a problem with shell syntax. For example, the following gives an error in bash:
Code:
echo -en "Enter the logmsg="  {D1234,S1234 | rohit singh}
read logmsg

# 3  
Old 10-14-2011
the input was only D1234,S1234 | rohit singh no braces so give the example in braces

Thanks
# 4  
Old 10-14-2011
Quote:
so I want can soneone help me how we can induct these two functions in single.
Not sure what you mean but I assume that you want to call these two functions.

If this is the case, simply add the following lines to the end of your script:
Code:
length
Char

# 5  
Old 10-15-2011
Code:
echo -en "Enter the logmsg="  {D1234,S1234 | rohit singh}
read logmsg
logmsg1=${logmsg%%|*};
echo $logmsg1 |sed "s/$/,/g"|sed 's/[ \t]*$//'>pre-commit.config
logmsg_len2=$(echo ${logmsg} | awk '{print $2}');
logmsg_suffix="|";
 
length()
{
cat "pre-commit.config"| awk -F'[,|]' '{for(i=1;i<NF;i++) { if (length($i) <= 6) { print " success" } else { print " Defect|Story number should not be more than 6 Characters i.e. D12345 or S12345";exit 1;}}}';
}
 
Char()
{
echo $logmsg1 | tr ',' '\n' | awk '{if($0~/^D/||/^S/) {print " "} else {print " Defect|Story number should be started with D or S";exit 1;}}'
}


length
Char

I know how to run the function but my question was that when I run this it doesn't full fill my requirement
because the problem in awk that it doesn't exit from the script and my requirement was if function first fails then it doesn't run the next function and it runs the next function that gives the problem


In the above example I have given the output

1) variable should start with D and S
2) IT SHOULD not more than 6 characters.

I want these two in single function but not with awk command.
# 6  
Old 10-15-2011
Quote:
I know how to run the function but my question was that when I run this it doesn't full fill my requirement
because the problem in awk that it doesn't exit from the script and my requirement was if function first fails then it doesn't run the next function and it runs the next function that gives the problem

In the above example I have given the output

1) variable should start with D and S
2) IT SHOULD not more than 6 characters.
Why did you not tell us this in the first place? We cannot read your mind!

Well, you cannot have the first letter starting with both 'D' and 'S', so I assume that you mean either 'D' or 'S'.

Try the following:
Code:
while :
do
   echo -en "Enter the logmsg="
   read logmsg
   if [[ ${#logmsg} < 7 && ( ${logmsg:0:1} == "D" || ${logmsg:0:1} == "S" ) ]]
   then
      break
   fi
   echo "ERROR: Must be 6 or less characters, starting with either \"D\" or \"S\""
done

# rest of your code

# 7  
Old 10-16-2011
D1234,S1234 | rohit singh

The above function will separate the variable?I think it will calculate the length of whole string
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

A single statement without main function in c

A sample.c file is written with only one single statement. main; Segmentation fault occurred when executed that file. Any statement other than main; is written, for example unix; then it won't compile. why is this behaviour ! (2 Replies)
Discussion started by: techmonk
2 Replies

2. Shell Programming and Scripting

Avoid single line output from function

I am new to shell scripting and wished to get few things clarified. While calling functions within shell script, output comes out as single line irrespective of the no of echos or newlines I tried within function + the echo -e used to invoke function ( as instructed online) : #!/bin/sh inc() {... (1 Reply)
Discussion started by: RMath
1 Replies

3. Shell Programming and Scripting

Pass parameters to a function and running functions in parallel

Hi , I have a script which is using a text file as I/P. I want a code where it reads n lines from this file and pass the parameters to a function and now this script should run in such a way where a function can be called in parallel with different parameters. Please find below my script, it... (1 Reply)
Discussion started by: Ravindra Swan
1 Replies

4. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

5. AIX

Calling functions from main program from dlopened library function

Hello All, I am trying to call a function from the calling main program from a dlopened library function, below is the entire code, when I execute it it crashes with sigill. Can you guys help me out I guess I am missing out on the linker flag or something here. besides I am new to AIX and... (1 Reply)
Discussion started by: syedtoah
1 Replies

6. Shell Programming and Scripting

Is it possible make the shell read functions 1 by 1 and calling an other function?

Greetings, I m wondering if it's possible do do the following : I have a simple function called "FindMoveDelete" which does the following : FindMoveDelete() { find . -iname "$FILENAME*.ext" -exec mv {} "$PATH/$VAR" \; && find . -maxdepth 1 -type d -iname "$FILENAME*" -exec rm -rf {}... (6 Replies)
Discussion started by: Sekullos
6 Replies

7. Shell Programming and Scripting

How to get the text of a single shell function?

I want to pass the text of a bash shell function to a remote machine in such a way that I can execute that function on the remote machine. For example, I can do this in a script ssh some_machine "tfunc () { echo Hello from \$(uname -n) ; } ; tfunc ; " and the function tfunc() will be... (9 Replies)
Discussion started by: MarkHamilton
9 Replies

8. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

9. Shell Programming and Scripting

Call single function multiple times diff set of parameters

Okay, not sure if it can be done, I would think it could be done and I'm just having a hard time with it. fun_close_wait(){ ipVar="${1} ${2}" portVar=`cat "${5}" | cut -d' ' -f 1` for ip in $ipVar do for port in $portVar do netstatVar=`netstat -n | grep... (4 Replies)
Discussion started by: cbo0485
4 Replies

10. Shell Programming and Scripting

Error in functions : file2: function: not found

Hi I had written the small script for calling a function. bash-2.03$ more file2 function sai { echo " this is an example" } echo "This is main program" echo "calling the function" sai() when executing the above script. I am getting error. bash-2.03$ sh file2 file2: function:... (2 Replies)
Discussion started by: padarthy
2 Replies
Login or Register to Ask a Question