Problem with writing into a file through shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with writing into a file through shell script
# 1  
Old 05-24-2011
Problem with writing into a file through shell script

Hi all,

I have a shell script which I use to login to the server from the client and then from the server I run a bunch of other scripts to complete my task. I am having problems with the script below-
Code:
#!/bin/bash

while read line
do
connections=`echo $line | cut -d " " -f 1`
period=`echo $line | cut -d " " -f 2`
testcaseid=`echo $line | cut -d " " -f 3`

ssh  192.168.1.202 <<EOF
sudo ./collectlcpu.sh $period $connections $testcaseid&
#sudo ./oprofile.sh $period $connections $testcaseid
sudo ./interrupt.sh $period $connections $testcaseid
#sleep 10
exit
EOF

./httperf1.sh $period $connections $testcaseid&
sleep 12

ssh  192.168.1.202  <<EOF
sudo killall collectl
sudo killall collectlcpu.sh
sudo killall interrupt.sh
sudo opcontrol --shutdown
sudo killall oprofile.sh
#killall pfmon.sh
#killall pfmon
exit
EOF
done  <  "/home/joydeep/input.txt"

sleep 30

The interrupt script is given below-
Code:
#!/bin/bash

dt=`date +%m%d%l%M`
dtstring=`echo $dt|sed 's/ //g'`

period=$1
connections=$2
testcaseid=$3
echo "STARTING THE INTERRUPT SCRIPT"
outputfile0="output_period_""Core2_""$period""_""connections""_""$connections""_""$testcaseid""_""$dtstring"".txt"
outputfile2="output_period_""Core0_""$period""_""connections""_""$connections""_""$testcaseid""_""$dtstring"".txt"
for i in 1 2 3 4 5 6 7 8 9 10 11 12
do
line1=`cat /proc/interrupts | grep eth3-TxRx-0`
line2=`cat /proc/interrupts | grep eth2-TxRx-0`
nohup echo $line1 >> /home/joydeep/results/interrupt/samesocket/data_cache_misses/"$outputfile0"&
nohup echo $line2 >> /home/joydeep/results/interrupt/samesocket/data_cache_misses/"$outputfile2"&
sleep 1
done
echo "END OF THE INTERRUPT SCRIPT"

The problem is that the script is not writing the files in /home/joydeep/results/interrupt.

Thanks,
Joydeep

Last edited by Franklin52; 05-25-2011 at 07:20 AM.. Reason: Please use code tags
# 2  
Old 05-25-2011
Please use code tags.

What's your reason to do this
Code:
line1=`cat /proc/interrupts | grep eth3-TxRx-0`
nohup echo $line1 >> /home/joydeep/results/interrupt/samesocket/data_cache_misses/"$outputfile0"&

instead of much simpler
Code:
grep eth3-TxRx-0  /proc/interrupts >> /home/joydeep/results/interrupt/samesocket/data_cache_misses/"$outputfile0"

?

I mean why the 'nohup' and sending it in background? You're just calling echo; it takes a split of a second....

Do you get the output (files created) when you invoke the interrupt script from command line?
# 3  
Old 05-25-2011
Hi,

Thanks for replying.

You are absolutely right- there's no reason for a separate line1 variable. Also the nohup is not needed. I was trying those options just to see whether they made a difference. What I am trying to do through this script is to login to the server and logging in the interrupt values from the output of /proc/interrupts in the output files when the httperf script is running.

But the problem is that the output files are not getting created in this manner. The output files get created when the interrupt script is evoked from the command line. Any help in this issue would be much appreciated.

Regards,
Joydeep
# 4  
Old 05-25-2011
To execute a local script on remote machine, you'd do:
Code:
ssh 192.168.1.202 bash < localScript.sh

I am not sure how to pass arguments to localScript.sh this way... maybe someone else can show us.

This should work though:
Code:
./echoInterrupt.sh $period $connections $testcaseid |  ssh 192.168.1.202 bash

This will redirect the output of echoInterrupt.sh and execute it (the output) on the remote machine.
echoInterrupt.sh should look something like this:
Code:
#!/bin/bash  

dtstring=`date +%m%d%l%M |sed 's/ //g'`  
period=$1 
connections=$2 
testcaseid=$3

cat <<EOF #print the commands to be executed

echo "STARTING THE INTERRUPT SCRIPT"

outputfile0="output_period_Core2_${period}_connections_${connections}_${testcaseid}_${dtstring}.txt"

for i in \$(seq 12) ; do   
   grep eth3-TxRx-0 /proc/interrupts >> /home/joydeep/results/interrupt/samesocket/data_cache_misses/$outputfile0 
done 

echo "END OF THE INTERRUPT SCRIPT"
EOF

You have to escape all dollar signs, that you want to pass as literal dollar signs to server, with backslash, as in the '\$(seq 12)' string: you want to pass '$(seq 12)' to the server.

When you run
Code:
./echoInterrupt.sh

it should print all the commands as you want them executed. So for debugging, just do ./echoInterrupt.sh and look at the terminal output to check for syntax errors.

You should not need sudo, since /proc/interrupts is world-readable.

Last edited by mirni; 05-25-2011 at 05:34 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

2. Shell Programming and Scripting

Sqlplus inside shell script writing to tmp file

Hi, facing an issue while calling sqlplus inside shell script. It for some reason goes to tmp file to write something and i get error as permission denied as i dont have access there. ANy idea why sqlplus writes in /tmp and how to change or stop this ? (2 Replies)
Discussion started by: rushikeshs
2 Replies

3. Shell Programming and Scripting

problem writing a simple c shell script

#!/bin/csh echo hello world this is what i got in a text file called ss1. i type "chmod 755 ss1.txt" to make it executable. then when i type ss1 or ss1.txt it says "ss1 command not found" what am i doing wrong? (19 Replies)
Discussion started by: pantelis
19 Replies

4. Shell Programming and Scripting

newbie: writing ksh shell problem

my default profile is using ksh, I tried to write a simple scripts and I had issues, below is my scripts: $ more if_num.ksh USAGE="usage: if_num.ksh" print -n "Enter two numbers: " read x y if ((x=y)) then print "You entered the same number twice." when I tried to executed the... (6 Replies)
Discussion started by: matthew00
6 Replies

5. Shell Programming and Scripting

Help with writing shell script file

I am trying to prompt the user using tput command to read the information ( 5 last names, first names and grades) from the keyboard. Save the data in a file called student.txt. Sort the file by last name and display it on the screen My pseudocode is as follow: Pseudocode: Initialize... (1 Reply)
Discussion started by: jestaton
1 Replies

6. Shell Programming and Scripting

Shell Script to remove spaces while writing to the file

Hello Folks, I want to get the results from a SQL query which needs to be exported to a .txt file. My Script is something like #!/bin/ksh db2 connect to DATABASE user user_name using pwd; touch test.txt isResult=0; isResult= `db2 -x select 'ABC',COL_B from TABLE_A WHERE COL_B=CONDITION`... (6 Replies)
Discussion started by: dinesh1985
6 Replies

7. Shell Programming and Scripting

Script writing problem

Self professed idot looking for help LOL Hi all, I am new to Unix and I have to write a shell script that will check to see if a file exist and then create it if it does not. The file I need to search for is titled "A1. dat" and here is my feeble attempt at creating the script: #!/bin/bash... (2 Replies)
Discussion started by: Tinablue
2 Replies

8. Shell Programming and Scripting

Shell script for a writing the directory structure to a file

Hi All, I am new user of shell scripting has come up with a problem. that I have a directory structure like : Home | |--------A | |----trunk | |-------A_0_1/ | | | |-------A_0_2/ | |--------B | ... (6 Replies)
Discussion started by: bhaskar_m
6 Replies

9. UNIX for Dummies Questions & Answers

HELP! writing shell script with c++ file

how would i write a shell script to count number of one-line comments in a c++ file. please help with coding thank you. (1 Reply)
Discussion started by: deadleg
1 Replies

10. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies
Login or Register to Ask a Question