Bash script directly in terminal?
Is it possible to execute a shell script without first saving it into a file? If so, I'm not able to find the proper syntax on my own. I am able to make a script and then execute it. However I often find myself wanting to type things that aren't worth the trouble of saving even temporarily. F.i.:
Code:
for x in *.bar do foo $x done |
You're going to need either newlines or semicolons.
Code:
for x in *.bar; do foo "$x"; done |
You can run like this:
Code:
/bin/bash -c 'for x in *.bar; do foo "$x"; done' |
Absolutely as per the examples shown or where in my case I have a bunch of txt files in a directory I can do:
Code:
for i in *.txt; do echo $i; done; What I always say is that "Whatever you can do in a command line, you can do in a shell script." Because that's what a shell script is. However, I also do find that writing increasingly more complex stuff into just the command line becomes more of a hassle versus writing to a file and using that. Because over time, your scripts do more versus less. I also follow a template of sorts in that most/all of my scripts contain: Code:
#/!bin/sh What I do is I have a sub-directory named "testcode" and I put in all experimentations there. I keep that stuff, some of it is work efforts, some of it is efforts put forth here to answer a question, but it helps to recall and after all, even a 20-30 line script is just 500-600 BYTES, it's more worth it for me to save that stuff versus discard it. See the "My Bash Blog" link in my signature for some script info. |
I tried newline with an escape, but that didn't work, also the scripts I based my line on seemed to have superfluous newlines. Thanks for the answers.
|
Quote:
My first instinct was to ask if you are creating scripts from within Linux and using one of the common Linux text editors such as vi, gedit, emacs and to cite the for instance that I would not use LibreOffice Writer to create and edit a script. However, you've said that you desire to put it all on the command line. Well ... why did you try to escape a newline and why did you think you needed to specifically add a newline? Proper end of lines would be the SEMICOLON. I'll go back to my short example and show it as originally represented on one line and also as a file script, note the differences and the second variation which is allowable: Code:
for i in *.txt; do echo $i; done; Code:
#!/bin/sh Code:
#!/bin/sh Code:
for i in *.txt; do echo $i done; Code:
#!/bin/sh Code:
./test.sh: line 5: syntax error: unexpected end of file |
echo $i; done;
the missing ; causes a very tricky error, and as far as I know this kind of issues cannot be solved easily. So: echo $i done; is completely correct, there is nothing wrong with it. Therefore the syntax checker will go further and try to find the end of the for loop (actually it would be the pair of the keyword do: done). But not found at all, therefore it will say: unexpected end of file. Another possibility is (just for example) to write a never ending string (missing closing "): echo "$i; done; a good help is an editor using syntax highlight, because you will easily recognize the problem (in this case done will be colored as string, not as keyword). In such cases the syntax checker never can tell you what's wrong, just tell you something went wrong. |
Quote:
Quote:
|
Quote:
This saved me a lot of time hunting for that missing/mistyped character. |
All times are GMT -5. The time now is 05:02 AM. |