LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-26-2010, 06:34 PM   #1
mukul_d
LQ Newbie
 
Registered: Aug 2007
Posts: 6

Rep: Reputation: 0
Redirect output of a command to another file inside shell script


Hi,

I am writing a script in which I am using AWK to append to a line in a file and save the file. The command I am using is:
Code:
awk '{s=$0; if ( NR==4 ){s=s ":/usr/java/jdk1.6.0_19/bin" } print s;}' $appName > $appName.new
This works perfectly on the command line. However when I insert the same command in my shell script, the script ignores everything after the first $appName.

The $appName variable is an input to the script.

Similar thing happens when I use SED to replace a piece of text in another file in the same script.

Code:
sed 's/DEV/UAT/g' Properties>Properties.tmp
Again, this works perfectly on command line.

My shell is BASH & I am using Redhat Enterprise Linux.

My entire script is:
Code:
export APPPATH=~/Library/WebObjects/Applications
export APPNAME=$1

# Prepare the deployment env.
prep_env()
{
    typeset appName=$1
    cd ~/Builds
    if [[ -e $appName* ]]; then        rm $appName*
    fi
    curl -O http://smithers.apple.com/hudson/job/RJV-trunk-RJVApps-DEV/com.apple.rjv.app'$'$appName/lastSuccessfulBuild/artifact/com.apple.rjv.app/$appName/1.0.0.0-SNAPSHOT/$appName-1.0.0.0-SNAPSHOT-wod
ocroot.tar.gz

    tar -C ~/Backups/ -xf $appName*
}

append_path()
{
    # Modify the path declaration line in the Application launch file.
    typeset appName=$1
    typeset appPath=$APPPATH
    cd $appPath/$appName.woa
    exec awk '{s=$0; if ( NR==4 ){s=s ":/usr/java/jdk1.6.0_19/bin" } print s;}' $appName > $appName.new
# | mv $appName.tmp $appName | chmod +x $appName
}

modify_prop()
{
    #Modify the Environment Setting in the Properties file from DEV to UAT
    typeset appName=$1
    typeset appPath=$APPPATH/$appName.woa/Contents/Resources
    cd $appPath
    exec sed 's/DEV/UAT/g' Properties>Properties.tmp &&  mv Properties.tmp Properties
}

prep_env $APPNAME
append_path $APPNAME
modify_prop $APPNAME
 
Old 08-26-2010, 09:16 PM   #2
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
- How do you call your script?
- No need to use 'exec'
- You can use 'sed -i' to edit inline
 
Old 08-27-2010, 02:36 AM   #3
mukul_d
LQ Newbie
 
Registered: Aug 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Not using exec

I am actually not using exec. I found suggestions on some other threads on this forum about using exec. But it does not help.

I am not very well conversant with the sed command. I am just using basic features. I will check it out tomorrow.
 
Old 08-27-2010, 03:28 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Code:
awk '{s=$0; if ( NR==4 ){s=s ":/usr/java/jdk1.6.0_19/bin" } print s;}' $appName > $appName.new
I suggest you to check the value of "$appName" before running awk. For a quick check, 'echo "$appName"' will work.
 
Old 08-27-2010, 02:23 PM   #5
mukul_d
LQ Newbie
 
Registered: Aug 2007
Posts: 6

Original Poster
Rep: Reputation: 0
$appName resolves correctly.

Quote:
I suggest you to check the value of "$appName" before running awk. For a quick check, 'echo "$appName"' will work.
The same command works perfectly on command prompt. The $appName variable is the same used earlier in the script and it resolves correctly.

Also when I run the script in debug mode using -x, it shows $appName resolving to correct parameter. However, it does not show anything after the $appName i.e. the > $appName.new..

Why is this happening?
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to redirect the SCP command output to text file. gokuls Linux - General 10 02-22-2012 02:12 AM
Problem executing mv command inside shell script pablogosse Linux - General 7 12-16-2009 12:21 PM
invoking windows command inside a shell script nano2 Programming 6 09-09-2009 03:46 AM
Unable to redirect script output to a file alien12 Linux - Newbie 4 08-25-2009 03:36 AM
Shell scripting: How to redirect output from within the script itself? Arodef Linux - General 4 05-23-2006 07:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:27 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
Open Source Consulting | Domain Registration