Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

why is my command unable to append to a file?

Posted on 2014-03-24
4
Medium Priority
?
266 Views
Last Modified: 2014-03-24
when i run the script directly then I am able to append to the variable [FileName] $logFile

script1.sh

!/bin/sh
IPA_LOG=${IPA_LOG:=/tmp/oli}
monitor="gf"
process_pid="10162"
cmd_array=("cat /proc/${process_pid}/status")
logFile=${IPA_LOG}/$monitor.log

 fileType="mem"
 cmd_array[1]="lsof -s -p $process_pid | awk '{ user = \$4 } { if ( user == \"$fileType\" ) { print } }' | sort -nrk 7 |  head -100"

        for i in "${cmd_array[@]}"
        do
         eval ${i} >> $logFile
        done
---------------------------------------------------------------------------------------------------
but if called thru chain of script:

!/bin/sh
#scriptA
...
..
start()
{
scriptB &
}

!/bin/sh
#scriptB
...
..
doSomething(){
script1 &
}

Now the script1 is unable to append to the logFile variable. I have tried changing the chmod and chown options. But no use.
0
Comment
Question by:pvinodp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 39951834
How are you determining it isn't appending?  

Are there any error messages?

Is the IPA_LOG variable defined?

Add a

set -x

Open in new window


near the top of script1 and post the output.

BTW, I assume you typed the scripts here as they contain errors.  Always best to copy/paste.
0
 

Author Comment

by:pvinodp
ID: 39952243
ok. let me put it more clearly:
following is the outline of script1.sh.

!/bin/sh
pid=$1
type=$2

functionA(){

  while :
  do

    if[cpu_id -gt threshold_const]
    then
       for i in "${cmd_array[@]}"
        do
         eval ${i} >> $logFile
        done
    fi

  done
}

if [x$type == xgf]
then
#accordingly i populate the cmd_array and logFile variable
functionA
fi

if [x$type == xok]
then
#accordingly i populate the cmd_array and logFile variable
functionA
fi

If I call this script by passing the 2 arguments the logs are not appending. But I can see on the filesystem that the timestamp of the logFile is updated.

If i just remove all the code and just keep the following then I see that the logFile is appended.

!/bin/sh
IPA_LOG=${IPA_LOG:=/tmp/oli}
monitor="gf"
process_pid="10162"
cmd_array=("cat /proc/${process_pid}/status")
logFile=${IPA_LOG}/$monitor.log

 fileType="mem"
 cmd_array[1]="lsof -s -p $process_pid | awk '{ user = \$4 } { if ( user == \"$fileType\" ) { print } }' | sort -nrk 7 |  head -100"

        for i in "${cmd_array[@]}"
        do
         eval ${i} >> $logFile
        done


The only diffrence here is that the logFile is static and there is no while loop. Do u see any leak somewhere?
0
 
LVL 48

Accepted Solution

by:
Tintin earned 2000 total points
ID: 39952253
Inside functionA

add

set -x

that way you can see exactly what it is attempting to do.
0
 

Author Comment

by:pvinodp
ID: 39952303
thanks for the input...
inside the function i have another line which has > instead of >>
thanks for suggesting to define "set -x" inside the function
0

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question