Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Simple shell script to execute 2 unix cmds

  • 27-04-2007 9:51am
    #1
    Registered Users Posts: 264 ✭✭MartyM


    Hi All

    I'm new to boards.ie and posted this question in the "Programming" forum last night, having just found this forum, perhaps this is where I should have posted...

    I'm having problems creating a simple shell script to execute 2 unix commands.

    Basically, the commands are:

    > cleartool setview myview
    > source /vobs/mydir/...

    Setting the clearcase view is causing the problem, this command seems to launch a subshell (i.e. the SHLVL environment variable is incremented), so the 2nd command does not get executed until I exit the subshell.

    How do I get the 2nd command to execute in the subshell?

    The commands must be in this order because the /vobs/... dir is not accessible without the clearcase view being set.

    btw....I use tcsh

    Any help with this would be great!

    Cheers!


Comments

  • Registered Users Posts: 5,112 ✭✭✭Blowfish


    Do you actually have to exit the subshell for the other command to work?

    If not, how about just running it in the background. i.e.

    > cleartool setview myview &
    > source /vobs/mydir/...

    You'd then have to add another command or two to kill the subshell though.


  • Closed Accounts Posts: 1,806 ✭✭✭ Khalil Early Pilgrim


    MartyM wrote:
    Hi All

    I'm new to boards.ie and posted this question in the "Programming" forum last night, having just found this forum, perhaps this is where I should have posted...

    I'm having problems creating a simple shell script to execute 2 unix commands.

    Basically, the commands are:

    > cleartool setview myview
    > source /vobs/mydir/...

    Setting the clearcase view is causing the problem, this command seems to launch a subshell (i.e. the SHLVL environment variable is incremented), so the 2nd command does not get executed until I exit the subshell.

    How do I get the 2nd command to execute in the subshell?

    The commands must be in this order because the /vobs/... dir is not accessible without the clearcase view being set.

    btw....I use tcsh

    Any help with this would be great!

    Cheers!

    My guess would be that cleartool is a shell script that exports a variable that your source command reads. Type "type cleartool" and open the file it references in vi to see what it is doing.
    It could be that the cleartool script does not work in tcsh i.e. is bash specific.


  • Registered Users Posts: 264 ✭✭MartyM


    Yeah....when running it in a script I have to exit the subhell before the 2nd command is executed. Here is what it looks like at the moment:

    > test1.sh
    [myview] > exit
    exit
    /vobs/mydir: No such file or directory.
    Exit 1
    >

    # On exit from the subshell (myview prompt), the 'source' cmd is executed.

    # Manually it should work like this:

    > cleartool setview myview
    [myview] > source /vobs/mydir


    # I also tried your suggestion of running it in the background but it doesnt like that...

    > test2.sh
    [1] 25949
    /vobs/mydir: No such file or directory.
    Exit 1
    >


  • Registered Users Posts: 264 ✭✭MartyM


    PaschalNee wrote:
    My guess would be that cleartool is a shell script that exports a variable that your source command reads. Type "type cleartool" and open the file it references in vi to see what it is doing.
    It could be that the cleartool script does not work in tcsh i.e. is bash specific.


    Cleartool works fine in tcsh......at least when the commands are issued manually!!

    The cleartool file is an executable and unreadable...


  • Closed Accounts Posts: 1,806 ✭✭✭ Khalil Early Pilgrim


    OK based on that I'd try
    echo "source /vobs/mydir" | cleartool setview myview
    

    or create a text file with your commands e.g.
    source /vobs/mydir
    blah blah 
    blah 
    exit
    

    and do
    cleartool setview myview < commands.txt
    


  • Advertisement
  • Registered Users Posts: 264 ✭✭MartyM


    PaschalNee wrote:
    OK based on that I'd try
    echo "source /vobs/mydir" | cleartool setview myview
    

    or create a text file with your commands e.g.
    source /vobs/mydir
    blah blah 
    blah 
    exit
    

    and do
    cleartool setview myview < commands.txt
    

    Thanks for that but I'm afraid both suggestions returned nothing....the normal command prompt returned in both cases after about 3 seconds....even when I manually entered "cleartool setview myview < commands.txt"????


  • Closed Accounts Posts: 1,806 ✭✭✭ Khalil Early Pilgrim


    >the normal command prompt returned in both cases after about 3 seconds
    Sounds like it worked to me - what were you expecting to happen


  • Registered Users Posts: 264 ✭✭MartyM


    Maybe someone knows another way around this?

    The prerequisite for all of this is that I launch my xterms as follows:

    xterm -sb -sl 10000 -title clearcase -bg lightgreen -e cleartool setview myview &

    This launches my clearcase xterm, i.e.

    [myview] > I then work away in clearcase...

    Now I want to add a second arguement (source /vobs...), so when I launch my xterm I dont have to type it manually...


  • Registered Users Posts: 264 ✭✭MartyM


    PaschalNee wrote:
    >the normal command prompt returned in both cases after about 3 seconds
    Sounds like it worked to me - what were you expecting to happen

    I need my shell to enter clearcase mode....for want of a better description!

    i.e.

    > go from this prompt

    [myview] > to this prompt

    Basically, I need to continue working within clearcase after I have sourced my directory.....sourcing the directory is just setting up my environment.


  • Registered Users Posts: 264 ✭✭MartyM




  • Advertisement
  • Closed Accounts Posts: 1,806 ✭✭✭ Khalil Early Pilgrim


    Expect might do the trick
    expect -c "spawn cleartool setview myview; send \"source /vobs/mydir/\";interact "
    

    You can move the expect script into a file to get around needing to escape the double quotes.


  • Registered Users Posts: 264 ✭✭MartyM


    PaschalNee wrote:
    Expect might do the trick
    expect -c "spawn cleartool setview myview; send \"source /vobs/mydir/\";interact "
    

    You can move the expect script into a file to get around needing to escape the double quotes.

    I'm having problems executing this on the command line due to the double quotes.

    I need to look at this for a while because I dont know expect at all and I'm not even sure if its setup in my work environment...

    > type expect
    expect not found

    Thanks a lot!....and I'll let you know if I get it....


  • Registered Users Posts: 264 ✭✭MartyM


    Thanks PaschalNee for all your help....I still didnt get around to the expect but will definitely give it a go!!

    Ideally I wanted to remain in the clearcase envornment and have the
    equivalent of the following from the command line:

    > cleartool setview myview
    [myview] > source /vobs/setup

    [myview] > Then continue working
    [myview] > within the clearcase environment
    [myview] > and within the subshell spawned by the cleartool command


    However, the 2nd workaround on that site was sufficient.....for now!

    http://www-1.ibm.com/support/docview.wss?rs=984&context=SSSH27&dc=DB520&uid=swg21119859&loc=en_US&cs=UTF-8&lang=en&rss=ct984rational

    The following script allows me to set my view, source the vob and execute a
    command within the clearcase environment.

    The downside is that I dont remain in the clearcase environment.

    The upside is that I dont have to "enter" the clearcase environment when
    using the alias! :)

    #!/bin/tcsh
    
    # This script allows the user to run the 'extractinfo' command from the vobs
    # without having to set their view or source /vobs/setup manually.
    
    # The script is executed with the following command:
    #
    #	cleartool setview -exec ~<user>/bin/source_extractinfo.sh myview
    # 
    # OR with the alias 'lminfo' defined in ~<user>/.aliases
    
    # Source vobs so that the extractinfo utility can be used to extract LM information
    source /vobs/setup
    
    # Prompt the user to enter the location and name of the LM
    echo "Enter: /path/to/loadmodule"
    
    # Read user input from stdin
    set LM = $<
    
    echo "Performing 'extractinfo' on the following LM: $LM..."
    
    # Run the 'extractinfo' command to extract the LM info
    extractinfo $LM
    


  • Closed Accounts Posts: 2 W@


    Marty,

    to do what you want, you have to perform the actions in the same way that a cronjob would. when trying to run cleartool via a script or external operator, you have to use the full path to perform the action such as:
    /opt/rational/clearcase/etc/util/cleartool setview view /source/vobs/setup

    I'm not sitting on a UNIX box at the moment, so I can't be positive of the actual location of the cleartool path, but I know it is in /opt/rational/clearcase somewhere. I've run builds by setting up my view this way then running the maven utility as a follow on.

    If you want to pipe cleartool commands together, you could also use something similar to: for i in `cleartool command -options`; do cleartool command -options $i; done (i.e. "for i in `cleartool lsco -r -cview`; do cleartool ci -nc $i; done).


  • Closed Accounts Posts: 12,807 ✭✭✭✭Orion


    Umm - you just answered a 2 year old query.


  • Closed Accounts Posts: 2 W@


    2 or 20, the answer is there for anyone that may need it in the future. We'll all survive my lateness.


Advertisement