LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2013, 05:36 AM   #1
setevoy
LQ Newbie
 
Registered: Oct 2013
Location: Kiev, UA
Distribution: CentOS
Posts: 10

Rep: Reputation: Disabled
[Shell] Running "breake 2" command from parametr for function


Hello, people.

Have "simple" script.

In script has loop (cycle?) > function1 > if[...] > function2 > loop2.

function2 have second loop inside it:

Code:
function2 ()
    {
    while ... do
    ...
    done
    }
To end this sequence - function2 use $param4 with breake 2 command given in variable:

Code:
while function1; do
    ...
    function2 "$param1" "$param2" "$param3" "$param4
    ...
    done
And:

Code:
...
    param4="break 2"
    ...
Is it good idea - use so scheme? Is there any possible riskiness or something else "negative"?

Thanks fpor tips.
 
Old 10-30-2013, 06:14 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
I'm not sure I understand what you're trying to do. Can you post a simple, but complete example?
That said, it is not a good idea to put commands into variables (see this)
Also, is the break 2 command actually supposed to break out of the function? That doesn't seem to me to be a good design either.
 
Old 10-30-2013, 06:29 AM   #3
setevoy
LQ Newbie
 
Registered: Oct 2013
Location: Kiev, UA
Distribution: CentOS
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
That said, it is not a good idea to put commands into variables (see this)
Also, is the break 2 command actually supposed to break out of the function? That doesn't seem to me to be a good design either.
Thanks for link - it is really helpfull for me. Will read it later :-)

Quote:
I'm not sure I understand what you're trying to do. Can you post a simple, but complete example?
OK, here is full example.

Functions:

Code:
function answer ()
{
while read response; do
echo
case $response in
    [yY][eE][sS]|[yY])
        printf "$1\n"
        $2
        break
        ;;
    [nN][oO]|[nN])
        printf "$3\n"
        $4
        break
        ;;
        *)
        printf "Please, enter Y(yes) or N(no)! "
esac
done
}
Code:
function check_tomcat_status {
RUN=`ps aux | grep tomcat | grep -v grep | grep java | awk '{print $2}'`
}
Part of code where it's used:

Code:
printf "Checking Apache Tomcat status...\n\n"

while check_tomcat_status; do

if [ -n "$RUN" ]
then
printf "WARNING: Tomcat still running with PID $RUN. Stop it? "
answer "$tomcat_status_stoppping" "$tomcat_status_stop" "" "$tomcat_status_no"
sleep 2
        if [ -n "$RUN" ]
        then
                printf "WARNING: Tomcat still running. Kill it? "
                answer "$tomcat_status_stopping" "$tomcat_status_kill" "" "$tomcat_status_no"
                sleep 2
        fi
else
printf "Tomcat stopped, proceeding...\n\n"
break
fi

done
And variables:

Code:
tomcat_status_stop="/path_here/bin/shutdown.sh"
tomcat_status_stopping="Stopping Tomcat..."
tomcat_status_kill=". path_here/tom_kill.sh"
tomcat_status_no="break 2"
 
Old 10-30-2013, 08:22 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well not sure about anyone else but that seems even more confusing

Starting from the top down:

1. Look up case changing as no need to go through all those combinations for yes or no

2. Why not simply return the response from the loop and use a simple if to choose what to do

3. man pgrep - makes check_tomcat_status pointless

4. the point about placing commands in variables already pointed out and now we see not simply one but all 4 variables .. even sourcing from a variable. Maybe instead of using creative variable names in what seems to be an attempt to make the code something you can follow, simply add comments to your code
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] su - $user -c "command" starts shell instead of running command AlucardZero Linux - Software 3 08-28-2012 03:35 PM
How to set the font-size for "xterm" terminal-emulator (running "bash" shell) ajaygarg Linux - Desktop 1 03-09-2012 12:38 PM
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
Midnight Commander - "The shell is already running a command" BiThian Linux - Desktop 9 09-16-2006 12:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration