echo just 1 line before execution/set +-x


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting echo just 1 line before execution/set +-x
# 1  
Old 12-18-2007
echo just 1 line before execution/set +-x

Suppose that you want to mostly not echo commands inside your script during execution (e.g. to not bog the user down with details that they do not care about) but that there is the occaisional script line that you would like to echo before you execute it.

Is there an elegant way to achieve this?

The ugly way to do this is to have code like
Code:
<do stuff>
...
set -x
<execute desired command>
set +x
...
<do more stuff>

Here, you have to place set +- pairs around each desired echo line which is annoying.

Furthermore, the final set +x line gets printed out--is there any way to suppress that? In dos bat files you can suppress that by putting a '@' char at the start of any line that you do not want echoed when echo is on. Does the bourne shell have an equivalent?

The function that I wrote below works for simple commands, but it fails (in both cygwin and linux) for really complex ones (e.g. a long Java program invocation with lots of arguments and a big classpath).
Code:
# Echoes the command first and then executes it.
# Any number of parameters may be supplied.
# Inside this function they will first be concatenated into a single string with a single space between each arg.
# This string must not be empty or all whitespace
echoThenExecute() {
	# concatenate all the individual args into a single string with a single space between the args:
	argsAsString=""
	while [ $# -ne 0 ]
	do
		argsAsString="$argsAsString ${1}"
		shift
	done

	if [ "$argsAsString" = "" ]; then
		echo "ERROR: empty command supplied"
		exit 1
	elif [ ! -n "`echo $argsAsString | sed 's/[[:space:]]*//g'`" ]; then	# note: the final g in the sed command makes it a global replacement, which is crucial; see http://www.grymoire.com/Unix/Sed.html
		echo "ERROR: all whitespace command supplied"
		exit 1
	fi

	echo "$argsAsString"
	bash -c "$argsAsString"
}

# 2  
Old 12-18-2007
Maybe just invoke another shell with the trace on:

Code:
bash -xvc 'ls -l *'
 -or-
(set -x; ls -l *)

The problem with this being that it's a subshell and may act different. You also can't set values and expect them in the parent.

I frequently see things like this:

Code:
typeset CMD="cat | sed | grep | cut | awk -v foo.bar"
echo "$CMD"
eval $CMD

But that's really not much different from your "echoThenExecute()".
# 3  
Old 12-19-2007
Thanks for the subshell idea. Its clever, but I agree that it is not a perfect solution.

Just out of curiosity, what is the purpose of the typeset in the code below?

Quote:
Originally Posted by gus2000
Code:
typeset CMD="cat | sed | grep | cut | awk -v foo.bar"
echo "$CMD"
eval $CMD

Since you are giving no options to typeset, you could simply say
Code:
CMD="cat | sed | grep | cut | awk -v foo.bar"

correct?
# 4  
Old 12-19-2007
With regard to the original question, I would simply change:
command
to be:
echo "command"
command

Low tech, I know. But sometimes that's the way to go. Depending on the quotes in the original command I might need to fiddle with the quoting.

And "typeset" in a function creates a local variable.
Code:
$ cat typesettest
#! /usr/bin/ksh


function xyzzy
{
        typeset CMD=two
        echo in xyzzy CMD = $CMD
        return 0
}


CMD=one
echo CMD is $CMD
xyzzy
echo CMD still is $CMD
exit 0
$
$
$ ./typesettest
CMD is one
in xyzzy CMD = two
CMD still is one
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

set echo off and on

Hi I have written a bash script to capture the output of jmap. The command i execute is jmap -heap <pid> This gives details of memory usage of the process with <pid>. Now jmap not only gives this info but also prints couple more lines, which i am not interested in. Here are the lines that I... (3 Replies)
Discussion started by: avinthm
3 Replies

2. Shell Programming and Scripting

why the set rr='echo string|cut not working

I am new to the c shell script, can you let me know why the set rr= is not working. C shell script #! /bin/csh Set tt= 12345_UMR_BH452_3_2.txt set rr='echo $tt | cut –d”_” -f1' syntax error (4 Replies)
Discussion started by: jdsignature88
4 Replies

3. Linux

set echo off command issue

Hi all, I am executing a Oracle SQL statement in a shell script and spooling the output of the query into a File with spool command. I want to ensure that only output of the query appears in file excluding the SQL statement but even set echo off command is not working. Please help (7 Replies)
Discussion started by: sumi_mn
7 Replies

4. Shell Programming and Scripting

Execution problem with repeat the same program with two set of same data

I got a program named as "fastq_to_fasta". I got a long list of file all named as AB1 and AB2. My input file is : 071022_L1_AB1.fq 012121_L1_AB1.fq 021213_L1_AB1.fq 012153_L1_AB1.fq 071022_L1_AB2.fq 012121_L1_AB2.fq 021213_L1_AB2.fq 012153_L1_AB2.fq . . . . . . My desired... (10 Replies)
Discussion started by: patrick87
10 Replies

5. Shell Programming and Scripting

Problem while execution of second set of commands

Hi, I have a shell script with code . perf.env cd $QRY_DIR for SHELL_FILE in sql1 do export SNAME=$SHELL_FILE ${SCRIPT_DIR}/perf_qry.sh ${SPOOL_DIR} ${DB_ENVNAME} ${NAME} & RC=$(expr ${RC:-0} + $? ) sleep 60 if then echo sysdate>test1 echo query1.txt>>test1 grep -i... (6 Replies)
Discussion started by: ran16
6 Replies

6. Shell Programming and Scripting

Problem while execution of second set of commands

Hi, I have a shell script with code . perf.env cd $QRY_DIR for SHELL_FILE in sql1 do export SNAME=$SHELL_FILE ${SCRIPT_DIR}/perf_qry.sh ${SPOOL_DIR} ${DB_ENVNAME} ${NAME} & RC=$(expr ${RC:-0} + $? ) sleep 60 if then echo sysdate>test1 echo query1.txt>>test1 grep -i... (0 Replies)
Discussion started by: ran16
0 Replies

7. UNIX for Dummies Questions & Answers

Set User ID on execution mode.

I have a directory. To this directory, for Group bits combination, it is showing as 's'. Which I found out, it means "Set User ID on execution mode". Within this directory I am not able to create subfolder. Does it mean, only the Owner of this directory will be able to create subdirectories &... (5 Replies)
Discussion started by: videsh77
5 Replies

8. UNIX for Dummies Questions & Answers

set variable to Home, then echo it to screen

Major Newbie here folks. I'm trying to set a variable to my Home directory and then echo it to the screen. Any and all help is greatly appreciated. Thanks Anna (3 Replies)
Discussion started by: amidget
3 Replies

9. Shell Programming and Scripting

How to set echo on

I'm looking at my bash man page and I'm expecting to find some option that I can use to make it echo every command that it executes. The description of --verbose was pretty terse! Is --verbose supposed to make it echo every command it executes? My bash script script (named ws2) contains a... (1 Reply)
Discussion started by: siegfried
1 Replies

10. UNIX for Dummies Questions & Answers

Question on Set User ID on Execution

I want group to run one of my unix script as me . I when the script is run it should have my permission's not the group . I tried this : 1.Have a file called y.ksh cat y.ksh echo $LOGNAME >ak_test 2.Output file has this permission -rwx------ ak_test 3.Then I chnaged... (7 Replies)
Discussion started by: akrathi
7 Replies
Login or Register to Ask a Question