LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-12-2010, 04:36 PM   #1
vjy
Member
 
Registered: Oct 2004
Posts: 31

Rep: Reputation: 0
script help


I am trying to write a script. I am a newbie, so please excuse me if its easy.

I need to goto a folder, open a script file (same file every time). The file has different function calls, I need to comment all other function calls and just un-comment the one I want to use.

Eg

#call_1
call_2
#call_3
#call_4

it should always use the call_2 for this, but other programs or users can comment/uncomment these calls. So I need to make sure of it. Also if the calls are commented, then leave it alone, not add another #. Then save the file as the same file.

This script will be executed from a java class. So how can I go about this. Any suggestions are appreciated. Thanks.
 
Old 08-12-2010, 04:47 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
I cannot help with anything to do with the java end of things, but if I were writing a script that would do what you wanted to do for the commenting of functions, I would use sed.

Here's what I'd do:
Code:
sed 's/\(^[a-z].*\)/#\1/;s/#call2/call2/'
So, there are two sed expressions there, separated by a semi-colon. The first one comments any line beginning with a letter (a-z), resulting in all lines being commented. The second one un-comments call2 if it is commented.
Here's a working example:
Code:
sasha@reactor: echo "#call1
#call2
call3
#call4" | sed 's/\(^[a-z].*\)/#\1/;s/#call2/call2/'

# Output:

#call1
call2
#call3
#call4
sasha@reactor:
So, I've fed in a line of calls, with call3 un-commented. In the output, you can see that call3 has been commented out, and call2 is now un-commented.

To make sed edit a file in-place, do it like this:
Code:
sed -i 's/\(^[a-z].*\)/#\1/;s/#call2/call2/' filename
Note the -i argument, which tells sed to edit-in-place the file called "filename"

Hope this helps.

Sasha

Last edited by GrapefruiTgirl; 08-12-2010 at 04:48 PM.
 
Old 08-12-2010, 04:50 PM   #3
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 5.4, Mac OS 10.4 (tiger)
Posts: 1,005

Rep: Reputation: 79
You haven't said what your goal is and what your problem is, thus answering the question is a bit tricky. I would suggest that manually editing code is a bad solution to begin with.

What you could do instead is say have a case statement and then pass an argument or a storage of some sort to check to base it's actions off of. an example would be /etc/init.d scripts what often take arguments like start, stop and restart (some take status as well).
 
Old 08-12-2010, 07:28 PM   #4
vjy
Member
 
Registered: Oct 2004
Posts: 31

Original Poster
Rep: Reputation: 0
Thanks for the replies.

GrapefruiTgirl, actually the file contains a lot more lines. The function calls are only a part, so I need to find the call_2, then comment/uncomment that and then make sure all other functions are commented too.

I apologize if I did not explain it correctly.

r3sistance, I am trying to automate a long manual process. Now, we need to login to the Linux box, open the file, uncomment the function and then run it. But if I automate, the other users can do it themselves. Since I need to do for many such boxes, it becomes tedious.

Thanks again for the replies. Any other ideas?
 
Old 08-12-2010, 07:41 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Ummm ... this doesn't seem to make too much sense to me; why
do you have to comment/uncomment function names rather than
just USING them?


Cheers,
Tink
 
Old 08-12-2010, 08:23 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
FWIW, I'm pretty much with the other folks here, in that I don't fully get the point. However, I (as can the others here) can still help you. But, if we are to efficiently help you further, it would be good to have a look at the real file we're operating on here, or a reproduction of it which accurately shows what exactly has to be commented. If the various function calls actually have all sorts of different names, for example, then we'd need a more complex method of seeking them out and commenting/un-commenting the right ones.

Tinkster makes a good point above: I wonder why these function calls are getting switched around (commented and un-commented) over and over; why can't the file stay the same, and just get used differently? Maybe it would be better to call the script with an argument indicating which function call to use, rather than repeatedly editing the file? I mean, does each user have his/her own special function call?

Anyhow, if we have a better grip on the whole point of this, we may be able to suggest a better way of accomplishing what you're trying to do.
 
Old 08-12-2010, 09:37 PM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by Tinkster View Post
Ummm ... this doesn't seem to make too much sense to me; why
do you have to comment/uncomment function names rather than
just USING them?
Probably because a static program (java class) always access a single script and he wants to be able to customize the way things function by modifying the script. He wants to customize things not by the java class but by the script. Anyway I think it's probably better if he'll make the script more static as well but depends its functions on a configuration or another script or something else. Maybe even just a variable if he really wants to just modify the script.

For example:

Code:
FUNCTIONCODE=0  ## this value can be modified.

function f0 {
    ....
}

function f1 {
    ...
}

....

case "$FUNCTIONCODE" in
0)
    f0 ...
    ;;
1)
    f1 ...
    ;;
...
esac
Something like that..

Last edited by konsolebox; 08-12-2010 at 09:38 PM.
 
Old 08-12-2010, 09:49 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
My concern with modifying the script repeatedly is that, if I understand the original post, multiple user(s) and/or applications can modify this script at any time. Can there be a race condition if two or more entities try to edit & use the script within the same time frame?
Anyhow, I like konsolebox' idea above. But further to that idea, what if $FUNCTIONCODE were passed to the script as a parameter? Can the java class do this? The parameter would be dependent upon what user or program was calling the script, so:
Code:
FUNCTIONCODE="$1"  ## this value is passed to script

function user1 {
    ....
}

function user2 {
    ...
}

....

case "$FUNCTIONCODE" in
user1)
    user1 ...
    ;;
user2)
    user2 ...
    ;;
...
esac
 
Old 08-12-2010, 09:55 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by konsolebox View Post
Probably because a static program (java class) always access a single script and he wants to be able to customize the way things function by modifying the script. He wants to customize things not by the java class but by the script. Anyway I think it's probably better if he'll make the script more static as well but depends its functions on a configuration or another script or something else. Maybe even just a variable if he really wants to just modify the script.

For example:

Code:
FUNCTIONCODE=0  ## this value can be modified.

function f0 {
    ....
}

function f1 {
    ...
}

....

case "$FUNCTIONCODE" in
0)
    f0 ...
    ;;
1)
    f1 ...
    ;;
...
esac
Something like that..

Well ... he wasn't very clear on what he desires the result
of what he's doing to be.

The java call can't be that static if he a) modifies the shell
script first and b) then calls the function by name.

If I had one piece of code with a range of ops it can perform
I'd either
a) use command line parameters to get what I desire
or
b) have a script that evaluates the name it was invoked
as and then use an appropriate 'case' to get the desired
result, and have a lot of aptly named symlinks to the
script in the same directory.



Cheers,
Tink
 
Old 08-12-2010, 09:58 PM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
But if it will accept arguments or parameters, how will the java program know what the user put as a parameter?

How 'bout if they just modify a configuration file instead. If an invalid value is fetched from an expected variable inside a configuration file due to bad modifications or race conditions, the script might just probably assume a default function? This way the function of the java class will still be guaranteed to work. Maybe also the script can have a backup file that will contain the previous value just in case a problem occurs on the true configuration file.

Last edited by konsolebox; 08-12-2010 at 09:59 PM.
 
Old 08-13-2010, 09:37 AM   #11
vjy
Member
 
Registered: Oct 2004
Posts: 31

Original Poster
Rep: Reputation: 0
Thanks for all the replies. I should have made it clear.

OK, the script handles multiple things. The same script is used for installing the product, installing the DB, making a backup, running the product, generate the reports and so on. The script is fixed and normally the first time all the functions are called sequentially. But on the further running the scripts the installing the product or DB does not run. We comment that part alone and make the others functions run.

What I am trying to do is for the business people to just run the product alone. Now, we have to do it for them. If I can make it as I described above, I will incorporate that from a java class. Java class calling the shell script after authentication, update the file/script via the shell script, then run the script.

At a time only one person will use it, its at different times they might change it. Like I will run the product now, another team can update the script to just use the load DB function alone, but no one will run simultaneously.

Am I making sense. Also the script cannot be changed, its set.

I will check to see if I can get another script with just that function call and code and from the program I will call the new script file.

Thanks.

Last edited by vjy; 08-13-2010 at 10:01 AM.
 
Old 08-13-2010, 08:54 PM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Why not just have two scripts? Or maybe you can also accept options like '-i' or '--init' so that it will only be used once.

For example:

Code:
#!/bin/bash

INITIALIZEMODE=false

while [{ $# -eq 0 ]]; do
    case "$1" in
    -i|--init)
        INITIALIZEMODE=true
        ;;
    *)
        echo "error: invalid argument: $1"
        exit 1
        ;;
    esac
    shift
done

if [[ $INITIALIZEMODE = true ]]; then
    # do the initializing here, or declare the initializing functions here

    # function a {

    # }
fi

# continue normal operations from here

...
 
Old 08-13-2010, 09:16 PM   #13
joec@home
Member
 
Registered: Sep 2009
Location: Galveston Tx
Posts: 291

Rep: Reputation: 70
From the sound so fit you just need a menu system that will inter-operate between bash and java, and from the sounds of it java will not allow for much interactivity with the end user and bash. So the question is wioll Java pass extended arguments when calling a script?

./my_script argument-1

So lets say I have 2 subroutines, I would just need to have java call ./my_script a ./my_script b or ./my_script c.

Code:
#####################################################
CMD=$(echo $1)

if [ $CMD == "a" ] 2>&1>/dev/null ; then 
	echo Run Program A
fi

if [ $CMD == "b" ] 2>&1>/dev/null ; then 
	echo Run Program B
fi

if [ $CMD == "c" ] 2>&1>/dev/null ; then 
	echo Run Program C
fi
#####################################################
 
Old 08-17-2010, 07:25 PM   #14
vjy
Member
 
Registered: Oct 2004
Posts: 31

Original Poster
Rep: Reputation: 0
Thanks for all the replies. I think its better to have a separate file instead of modifying the same file. I am talking with my manager to see if they are fine with 2 files.

Thanks again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Need help with script to organise files into folders as part of DVD backup script jasybee2000 Linux - Newbie 5 06-15-2009 08:29 PM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM
MySQL Updates With Null When Perl Script Run From Shell Script ThisGuyIKnow Programming 6 08-12-2008 10:56 AM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 11:48 PM


All times are GMT -5. The time now is 06:42 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration