Score:1

how to constrain bash to deal only with part of script

ws flag

When I am testing a bash script, I set an exit command to restrict all to only the first part of the code.

But bash allways deals with the whole script, gives errogr messages to code below of the exit command.

I have only the solution to outcomment all below of my debug-exit.

That is inconvenient, and that item exists since 30 years.

Is there in the meantime in bash something like __END__ in perl?

In some shell, resp. in Emacs, years ago, there was the possibiliy to highlight a part of code and to run an subshell on this highlighted code.

Such a way I cannot find in kubuntu 20.04, in vim.

But it would not be the wanted solution, since it needs a lot of work.

Regards antondhidh

hr flag
Can you add a short reproducible example of `exit` not working?
terdon avatar
cn flag
Can you explain why it is inconvenient to simply comment out the rest? Any decent editor, and of course all IDEs, will offer a simple shortcut to comment/uncoment code, so it's a matter of selecting it and pressing the shortcut. On my emacs, for instance, I can toggle commenting using the `C-c c` shortcut which is bound to `comment-or-uncomment-region`.
Artur Meinild avatar
vn flag
Build your script using functions. In this way, you can have lots of code, but only call the specific functions you need. For an easy fix, you can make all the part of the script you don't want to run a function, so you need to add: `unused() {` before and `}` after.
ar flag
Please [edit your question](https://askubuntu.com/posts/1394155/edit) and add how you call the script. For example, by double clicking, or by `./scriptname` or by `bash scriptname` or by some other means. Also include a short script. I cannot reproduce this problem with a 4 line script: `#!/bin/bash` ↵ `echo first line` ↵ `exit` ↵ `echo second line` ↵ for example.
terdon avatar
cn flag
@user68186 I think the OP is talking about having malformed lines after the `exit`, so your script wouldn't reproduce it, but I can't reproduce with malformed lines either. Bash reads the script line by line, so it shouldn't complain about bad lines after an exit call.
ar flag
@terdon I agree. What comes after `exit` malformed or not, should not be executed at all. I use `exit` in bash all the time, usually with an `if` condition without any problems whatsoever.
Terrance avatar
id flag
You can always run `bash -x ./scriptfile.bsh` to show you the lines as they run in your script.
waltinator avatar
it flag
Always paste your script into `https://shellcheck.net`, a syntax checker, or install `shellcheck` locally. Make using `shellcheck` part of your development process.
Anton Wessel avatar
ws flag
Thanks for many answers. The utility "shellcheck" I do now know, but bash should not have the described issue even with no internet and no shellcheck., but onl
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.