![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| new line in echo | koti_rama | Shell Programming and Scripting | 5 | 12-27-2008 03:29 PM |
| What's wrong with this line: if ${TEST:?} ; then echo empty; fi | meili100 | UNIX for Dummies Questions & Answers | 2 | 02-23-2008 11:45 AM |
| execution without using (./) | mehmetned | UNIX for Dummies Questions & Answers | 2 | 02-09-2007 10:28 AM |
| how to echo the file contents LINE BY LINE | newbie168 | Shell Programming and Scripting | 4 | 02-22-2006 06:43 AM |
| How to place the output of two different echo statements on one line | JoBa | Shell Programming and Scripting | 5 | 11-20-2002 10:54 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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"
}
|
|
||||
|
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()". |
|
||||
|
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:
Code:
CMD="cat | sed | grep | cut | awk -v foo.bar" correct? |
![]() |
| Bookmarks |
| Tags |
| bash, bash eval, eval, linux commands |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|