Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a way for running a shell script with echoing the commands but w/o actually executing them?

Let's say I have script removing a file whose name is stored in a variable:

#!/bin/bash
set -v
FN="filename"
rm -f ${FN}

Adding set -v will echo the following commands before execution:

$ ./scr.sh
FN="filename"
rm -f ${FN}

Now, I want to see the flow of this script w/o actually removing the file. IOW, I want to prevent any effect on the external environment and filesystem. Is this possible?

I could wrap all commands with an echo command, but it is tiring for a long script.

share|improve this question
    
I'm not a bash-scripting expert, but if I would write this with another programming language, I would store all commands in an array. this way it should be easy to loop through and either execute the commands or print the out. –  hugo der hungrige Mar 16 at 20:34
add comment

5 Answers

There's no way to step through a script to see how it would execute without actually doing that. In your example, there are no if statements or loops. But in real scripts, there are often lots of conditional statements. Which branch will get taken will often depend on what happened when the shell ran the previous command. If it doesn't run the command, there's no way for the shell to know what output it would have generated or what the return code would have been, which the next conditional branch or assignment statement might depend on.

If the point is that you'd like to scrutinize a script and know what it does before you run it, that's not a bad idea. But realistically, about the best way to do that is by just browsing the file with less or vi or something similar.

Added

If you're developing a script and you're wanting to step through it the first time, testing the logic but not actually doing any damage if you've got a bug, the solution I'm often able to use is to modify only the statements that could do any damage by pasting an echo onto the front.

This often works in typical real-life scripts because (a) generating the lists of items you'll iterate over or the value to which you'll set a variable can often be generated without changing the filesystem and (b) it's usually sufficient to assume that if you did run the command, it would succeed.

share|improve this answer
    
Thanks. It would surprise me if there is a way, exactly due to the reason you mentioned, but I decided to give it a try anyway. The reason for this is that I am developing a script that moves files across many machines in a network and performs some actions on the remote hosts. I'd like to see a flow of the script before actually committing the changes to the filesystem(s). It would be just as tedious to go over all the remote machines to correct file errors... –  ysap Jan 16 '13 at 19:57
add comment

You an trace the flow using the -x option for bash, but this would still execute the commands.

The best you could do is to just put echo or comment out the commands that have external effects like the rm. At least you wouldn't have to change every line.

share|improve this answer
    
Thanks. I mentioned that option in my question, but it does not really solve my problem. –  ysap Jan 16 '13 at 19:58
    
Sorry, I thought you meant echoing every line, -x would reduce this to a few commands and show you the evaluations. –  parkydr Jan 16 '13 at 20:06
add comment

I think you are going to have to echo - however if you put the command in a variable you can turn it on and off. Also, the command can be a function and as sophisticated as you want.

#!/bin/bash
echo 'Debug'
  D=echo
  :>| testy
  ls -l testy
  $D rm -f testy
  ls -l testy
echo $'\n\nLive'
  D=
  :>| testy
  ls -l testy
  $D rm -f testy
  ls -l testy

!$ > ./conditional.sh
Debug
-rw-rw-r-- 1 nobody nobody 0 2013-01-18 15:28 testy
rm -f testy
-rw-rw-r-- 1 nobody nobody 0 2013-01-18 15:28 testy

Live -rw-rw-r-- 1 nobody nobody 0 2013-01-18 15:28 testy ls: cannot access testy: No such file or directory

share|improve this answer
    
I required quotes in the echo/don't-echo commands, and then had to use D=eval in the live part. But nice answer! –  Jasper Apr 7 at 7:49
add comment

Do set –n, or append n to your existing set –v command.  But since this causes the shell not to evaluate any commands, not even if or while, you might not get too good a view of the operational flow.

share|improve this answer
    
Thanks. This is a good start, but as you mentioned, it does not let me see the actual flow - where in my real script I have a loop over a list of remote hosts. –  ysap Jan 16 '13 at 20:01
add comment

If you want to prevent modifications from happening, you can run the script as a user without privileges:

sudo -u nobody ./scr.sh

In your example, this will execute FN="filename" as usual. While it technically executes the command rm -f ${FN} as well, nothing will happen if nobody doesn't have the necessary permissions to delete the file.

Of course, this will cause problems with conditional workflow. The fact that the rm command failed might affect further parts of the script.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.