Help with a simple function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with a simple function
# 1  
Old 04-29-2012
Help with a simple function

Hi there

Everytime it calls the second if statement it keeps calling the first echo message from the first if statement.

how do I get it to display the second echo message in the second if statement?

Thanks.


Code:
function (verbose) { 

if [ "args_V" = v" ] ; then 
echo "rm: cannot remove $1 : is a directory"

elif [ "$args_V" = "v" ] && [ -f "$1" ] ; then 
echo "removed \`$1'"

if [[ "$args_V" = "v" &&  "$args_I" = "i" &&  "$args_R" = "r" ] then
interactive $1

fi 
fi 
}

# 2  
Old 04-29-2012
There are syntactical problems with your snippet. The function name cannot have parentheses, on the second line there is a double quote missing. I suggest you have another look at the syntax...

Last edited by Scrutinizer; 04-29-2012 at 09:34 AM..
# 3  
Old 04-29-2012
sorry i worte the code wrong here on the forum
still have the same problem as suggested above Smilie
Code:
function verbose () { 

if [ "args_V" = v" ] ; then 
echo "rm: cannot remove $1 : is a directory"

elif [ "$args_V" = "v" ] && [ -f "$1" ] ; then 
echo "removed \`$1'"

if [[ "$args_V" = "v" &&  "$args_I" = "i" &&  "$args_R" = "r" ] then
interactive $1

fi 
fi 
}

# 4  
Old 04-29-2012
And still there is a double quote missing on line 2 (and a $-sign?)
# 5  
Old 04-29-2012
Apologies for the mistakes on the forum,

still having the same problem? SmilieSmilie

Code:
function verbose () { 

if [ "$args_V" = "v" ] ; then 
echo "rm: cannot remove $1 : is a directory"

elif [ "$args_V" = "v" ] && [ -f "$1" ] ; then 
echo "removed \`$1'"

if [[ "$args_V" = "v" &&  "$args_I" = "i" &&  "$args_R" = "r" ] then
interactive $1

fi 
fi 
}

# 6  
Old 04-29-2012
Since elif is being used, the second condition does not get evaluated when condition 1 is true, and it always evaluates to false when condition 1 is false, so condition 2 never becomes true.....
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 04-29-2012
Oh i see however the second condition (elif) does evaluate so in this case it does remove the file but it doesn't execute the second echo statement. What can i do to fix this?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. UNIX for Beginners Questions & Answers

Simple shell script function

Hello, Trying to look vgcheck() { lsvg -o| while read vg; do lsvg -p $vg|grep Missing if ;then echo "OK:PASSED" else echo "FAIL" ... (3 Replies)
Discussion started by: kvosu
3 Replies

3. Shell Programming and Scripting

Wrong test interpretation for simple function.

Hello everyone, I have written simple script below to check if ip is added to interface #!/usr/local/bin/bash IFCONFIG="/sbin/ifconfig" SERVICE="/usr/sbin/service" IP="79.137.X.X" GREP=$(${IFCONFIG} | grep ${IP}) ip_quantity_check () { echo ${GREP} | wc -l } if ];... (2 Replies)
Discussion started by: bryn1u
2 Replies

4. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

5. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

6. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

7. Shell Programming and Scripting

Simple Shell Script using function

Hi all, I am new to shell scripting.I have made a simple shell script which will give me number of records in a database table. The SQL statement is working fine and there are 11 rows in this table. But problem is that it is not printing this value and fucntion does not get called. Please see the... (5 Replies)
Discussion started by: 33junaid
5 Replies

8. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

9. UNIX for Dummies Questions & Answers

simple function

hi all, I am new to Shell Programming. I had a simple function here: function xyz { print "test" } I save the file as "abc" and give X to all. After I type abc from the terminal, I don't get the "test" as output. Am I missing anything? Thank you for your input in... (3 Replies)
Discussion started by: andrec
3 Replies
Login or Register to Ask a Question