LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-18-2015, 04:33 AM   #1
Weapon S
Member
 
Registered: May 2011
Location: Netherlands
Distribution: Debian, Archlinux
Posts: 262
Blog Entries: 2

Rep: Reputation: 49
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
 
Old 02-18-2015, 05:19 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
You're going to need either newlines or semicolons.

Code:
for x in *.bar; do foo "$x"; done
 
1 members found this post helpful.
Old 02-18-2015, 06:21 AM   #3
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
You can run like this:

Code:
/bin/bash -c 'for x in *.bar; do foo "$x"; done'
 
Old 02-18-2015, 07:01 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,788
Blog Entries: 13

Rep: Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830
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;
And this is the same as a "ls -1 *.txt" command.

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

#set -xv
And I either enable the set -xv or not depending on whether I wish debug while I run the script.

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.
 
Old 02-19-2015, 03:58 AM   #5
Weapon S
Member
 
Registered: May 2011
Location: Netherlands
Distribution: Debian, Archlinux
Posts: 262

Original Poster
Blog Entries: 2

Rep: Reputation: 49
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.
 
Old 02-19-2015, 07:25 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,788
Blog Entries: 13

Rep: Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830
Quote:
Originally Posted by Weapon S View Post
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.
This is why I do the exact opposite of what you originally said about not taking the time to write them into a file and just use the command line. They'll be easier to debug.

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;
One form of fully expanded out. Note that there are newlines, however no semicolon characters because they're not necessarily needed:
Code:
#!/bin/sh

for i in *.txt
do
    echo $i
done
A briefer form where I've pulled the "do" up to the "for" line, it does require a semicolon to satisfy the syntax. Meanwhile the semicolons after the echo and done statements are tolerated, but not necessary:
Code:
#!/bin/sh

for i in *.txt; do
    echo $i;
done;
Now here's an example of mistakes in the one-line and fully expanded scripts:
Code:
for i in *.txt; do echo $i done;
>
The result here is the command line dropped me to a continue line where it is expecting more shell input. It correctly detects that the command sequence is not complete so it's waiting for the remainder of that sequence. As a result, the writer of that line has just about zero feedback as to what the problem is. Now take that same single line script and make a mistake in the multi-line file script form:
Code:
#!/bin/sh

for i in *.txt; do
    echo $i done;
The result when trying to run this script is:
Code:
./test.sh: line 5: syntax error: unexpected end of file
It at least gives some impression of the error you made and points to the suspected line. But also as you can see, this particular complaint is beyond the last line of the script; still it allows you to then say "Perhaps the problem then is in line #4 ..."

Last edited by rtmistler; 02-19-2015 at 07:27 AM.
 
Old 02-19-2015, 09:03 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,196

Rep: Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830
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.
 
Old 02-19-2015, 09:13 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,788
Blog Entries: 13

Rep: Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830Reputation: 4830
Quote:
Originally Posted by pan64 View Post
In such cases the syntax checker never can tell you what's wrong, just tell you something went wrong.
Yeah I picked a bad example, but then again it was a very limited script. This is all up to Weapon S really. I just felt they were being too limiting by trying to simplify the concept and write scripts on the command line. On one hand, if it's just a quick, nominal command, why not? On the other hand, are my variety of points about saving prior work, the fact that saving files isn't going to blow away storage space, and then the issues of debug

Quote:
Originally Posted by pan64 View Post
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).
Your recommendation? I use emacs and that does highlight, but I also name my scripts with .sh extension. Unsure if one has a file with no extension but which is a shell script, if it will auto-detect that. Mine did not when I gave it a brief try, although I suppose I could force the emacs mode to say it's a script.
 
Old 02-19-2015, 09:31 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,196

Rep: Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830Reputation: 6830
Quote:
Originally Posted by rtmistler View Post
Your recommendation? I use emacs and that does highlight, but I also name my scripts with .sh extension. Unsure if one has a file with no extension but which is a shell script, if it will auto-detect that. Mine did not when I gave it a brief try, although I suppose I could force the emacs mode to say it's a script.
I use personally vim, and I set the type manually if vi cannot recognize. The main point is you need to be familiar with that colors, otherwise that will not say anything for you. I do not know emacs very well but I think it is as powerful as vi (or geany or - more or less - any other modern editor).

This saved me a lot of time hunting for that missing/mistyped character.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script - command works directly in command line but not in script raoulcousins Linux - Newbie 6 08-21-2013 07:43 PM
executing shell script in terminal directly with a double click kushalkoolwal Programming 15 11-26-2012 02:32 PM
Push output string from script directly into bash / readline command input buffer GreyBeard Linux - Software 3 02-20-2012 11:26 PM
[SOLVED] How to make a bash script keep running in same terminal after it calls second script? JohnRock Linux - Newbie 4 06-25-2010 09:16 AM
Print directly to Windows network printer within a bash shell script jwillyuaz Linux - Newbie 1 02-12-2008 02:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration