Using script command inside a script or emulating it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using script command inside a script or emulating it
# 1  
Old 03-16-2016
Using script command inside a script or emulating it

My script needs to take a folder path to the location of a C program, and run the program until the user selects exit in his C program. The script needs to create a transcript to a text file while the C program is running.

Creating a transcript can be easily done with the script command but it requires the user to type exit. I want to end the transcript automatically when the user exits his program.

The next option I found was to use,
Code:
script w6.txt <<+
Lessons/codeGames/Workshop6/w6.out </dev/tty
exit
+

Problem here is the word "exit" becomes input to the C program.

I've also tried,
Code:
Lessons/codeGames/Workshop6/w6.out | tee w6.txt

Here the terminal goes to the next line and just sits there (no command prompt).

Any help/suggestions please?

Here's my current code;
Code:
!#/bin/bash

clear
source=$1
headers=0

if [ "$source" = "" ]
  then echo -n "Enter path to folder with C code: "
	read source
	printf "\n"
fi

echo Hello $(finger $(whoami) | head -1 | cut -f4 | cut -d':' -f2 | cut -d' ' -f2), Welcome to mkScript v1.0
sleep 1.5

printf "\n"
printf "Checking path validity.."

if [ ! -d $source ]
  then
	printf "FAIL\n"
	echo That directory does not exist! >&2
	printf "\n"
	exit 1
  else
	printf "OK\n"
	if ls -U $source/*.c 1> /dev/null 2>&1
	  then
		if [ $(find $source -name *.c | wc -l) -gt 1 ]
		  then
			printf "Error: Found more than one main source file(.c).\nThere can only be one source file.\n" 
			sleep 1.5 
			printf "\n"
	  	  else
			fileName=$(find $source -name *.c | awk -F/ '{ print $NF }' | cut -d'.' -f1)
				
			echo Found $fileName.c
			sleep 1.5
			printf "\n"

			echo Searching for Header files..
			sleep 1
			if ls -U $source/*.h 1> /dev/null 2>&1
	  	  	  then 
				echo Found $(find $source -name *.h | wc -l) files.	
				#printf "\n"
				find $source -name *.h | awk -F/ '{ print $NF }'
				headers=1
		   	  else
				echo No .h Header files found.
			fi
			sleep 1.5
			printf "\n"

			txtFile=$source/$fileName.txt

			echo Creating script $txtFile #Attemping to emulate script command
			sleep 1.5
			echo Script started on $(date '+%c') > $txtFile
			echo $(whoami) >> $txtFile
			
			if [ $headers != 1 ]
			  then
				cat $fileName.c >> $txtFile 
			  else
				cat $source/$fileName.c >> $txtFile
				cat $source/*.h >> $txtFile
			fi

			gcc -std=c99 -lm $source/$fileName.c -o $source/$fileName.out
			cd $source
			$fileName.out | tee >> $txtFile #I'm stuck here
			
			echo Done! Your script is at $source/$textFile
			echo Good Luck!!
			printf "\n"
		fi
	  else 
		echo No .c file >&2
		exit 1
	fi 
fi

P.S. – This C program has scanf (responds to user input), and also generates random numbers.

Last edited by syntax_eror; 03-16-2016 at 10:37 AM..
# 2  
Old 03-16-2016
How about using:

Code:
Lessons/codeGames/Workshop6/w6.out  | tee w6.txt

Edit: Sorry didn't see you'd tried this. I suspect your c program is detecting the stdout is not a terminal and is functioning differently.
Some script versions allow -c command eg:

Code:
script -c Lessons/codeGames/Workshop6/w6.out w6.txt


Last edited by Chubler_XL; 03-16-2016 at 06:27 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 03-17-2016
Quote:
Originally Posted by Chubler_XL
How about using:

Code:
Lessons/codeGames/Workshop6/w6.out  | tee w6.txt

Edit: Sorry didn't see you'd tried this. I suspect your c program is detecting the stdout is not a terminal and is functioning differently.
Some script versions allow -c command eg:

Code:
script -c Lessons/codeGames/Workshop6/w6.out w6.txt

YES! Smilie script -c is it! Thank you so much!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with ls command inside script

Hi , DIR1 has only one file with .txt extension , trying to get the size of that file using the following script #!/bin/ksh foldr_1="/etc/DIR1" #echo "$foldr_1" sze_fdr1=$(ls -ltr foldr_1/*.txt |awk '{ print $5 }') echo "$sze_fdr1" After executing the above script getting... (1 Reply)
Discussion started by: smile689
1 Replies

2. Shell Programming and Scripting

Can i use if else inside expect command in shell script?

hii,, I am trying to automate jira. during my scripting using bash script, in the terminal i got the terminal message like this: "Configure which ports JIRA will use. JIRA requires two TCP ports that are not being used by any other applications on this machine. The HTTP port is where you... (1 Reply)
Discussion started by: nithinfluent
1 Replies

3. Shell Programming and Scripting

Command timeout from inside script.

Hi, I've written a very robust script to get an external IP address from 'behind' a router. It uses many web pages randomly choosing which one/ones to use at run time. The "fetch the web page containing the IP address" is handled by either wget or curl both of which have their 'max time for the... (6 Replies)
Discussion started by: gencon
6 Replies

4. Shell Programming and Scripting

Using ssh command inside a script

Hi, I have a script file in server A. Inside the script file, I first have a ssh command that will connect to a remote server B. In the same script file itself, I have a sequence of commands that has to be run in server B. I am embedding these commands in the script file that I have in server A.... (2 Replies)
Discussion started by: mick_000
2 Replies

5. Shell Programming and Scripting

How to monitor a command inside shell script

Hi All, Is there any way to monitor a command inside shell script ? I have a script inside which I have a tar command which zips around 200GB data. tar zcvf $Bckp_Dir/$Box-BaseBackup-$Day.tar.gz * --exclude 'dbserver_logs/*' --exclude postmaster.pid --exclude 'pg_xlog/*' I want to... (3 Replies)
Discussion started by: sussus2326
3 Replies

6. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

7. Shell Programming and Scripting

keep mail command active inside a script on unix

well, I have a script with this code inside: nohup /usr/sbin/auditstream | /usr/sbin/auditselect -m -e "event== USER_Create || event== USER_Remove || event== USER_Change || event== GROUP_Create || event== GROUP_Remove || event== GROUP_Change || event== PASSWORD_Change " | /usr/sbin/auditpr -h... (4 Replies)
Discussion started by: iga3725
4 Replies

8. UNIX for Dummies Questions & Answers

Exit out of the Script Command inside a Script

I'm new to Linux. I have a bash script that invokes an executable. I'd like use the SCRIPT command inside the script and exit out of the script command after it writes to the file. Does this make sense? Below is an example of the contents of my script. #BEGIN SCRIPT script typescript... (6 Replies)
Discussion started by: jmungai
6 Replies

9. Shell Programming and Scripting

Run the command inside perl script

I have a command which will run fine in a unix command prompt. Can you tell how to interprete this command inside perl script...... The command is : perl -pe 's/(\|333\}.*)\}$/$1|1.6}/' FIA.txt This will search for the number 333 and appends 1.6 at the end of that line....... (1 Reply)
Discussion started by: vinay123
1 Replies
Login or Register to Ask a Question