Does it make sense to use Bash scripts?

I just finished my second year as a student at the university, so my "real" knowledge is not enough. I studied Java for my first year, continued working with Java and took C and simple Bash scripts of my second. This summer I am trying to learn Perl (help me). In the past, I swore a bit with Python.

My question is that we now have very readable, very rewritable scripting languages ​​like Python, Ruby, Perl, etc., why does anyone write Bash scripts? Is there something I'm missing? I know that in my linux window there are perl and python. Aren't they common enough? Is there something easier to do in Bash than in some other hlls?

+62
python comparison scripting bash perl
May 20 '10 at 8:20
source share
18 answers

Among other things, bash is useful when most of what you do is exchanging data and piping between different programs (many of which are also standard). And in many environments, bash (or at least the POSIX shell ) is available, but Perl and Python are not.

And, of course, you must distinguish between interactive bash and bash with a script. Ubuntu recently switched its default scripting shell ( #!/bin/sh ) to dash because it was much faster. However, Bash has some useful interactive features. No Dash ( Zsh is still better, IMHO).

+55
May 20 '10 at 8:22
source share
— -

Bash is incredibly useful in system administration, web application deployment, hash data crunching, delayed backups, even getting-things done every day just to name very few of them. I think it's too early for you to judge a “veteran IT soldier” like BASH.

EDIT googling around:

I would also like to mention that the TextMate successful OS-X program contains many bash machines inside.

+31
May 20 '10 at 8:22 a.m.
source share

The real difference between bash and python is that python is a general-purpose scripting language, and bash is just a way to run many small (and often very fast) programs from the series. Python can do this, but it is not optimized for it. Programs (sort, find, uniq, scp) can perform very complex tasks very simply, and bash allows these tasks to interact very simply with pipelines, washing out output from files or devices, etc.

While Python can run the same programs, you will be forced to run bash scripts in a python script to execute the same thing, and then you will get stuck with both python and bash. Both are great for themselves, but their mixture does not improve anything IMHO.

+23
May 20 '10 at 8:43 a.m.
source share

Bash scripts let you automate command line tasks using the same language if you manually enter commands.

A trivial example for the $ PATH list

Bash

 #!/bin/sh echo $PATH 

Python

 import os print os.getenv("path") 
+22
May 20 '10 at 8:39 a.m.
source share

For many tasks, bash can be very concise. Code for finding a line in all files of the current directory and printing these lines:

In bash:

 grep -ir "search_term" . 

In Python:

 for file in os.listdir('.'): file_content = open(file).readlines().split('\n') for line in file_content: if line.count("search_term") > 0: print file, ": ", line 
+14
May 25 '10 at 16:12
source share

I think the strong argument that python is not as natural as bash is for many tasks is a simple fact that no one uses the python interpreter as their shell. It is clear that this is possible:

 python> import os, subprocess python> os.chdir(os.path.expanduser("~/work")) python> subprocess.call(["vim","README"]) 

Clearly using python for a shell is absurd. bash was designed to run other programs. Python was designed to perform calculations. Although most things can be done in any language, there will always be tasks that can be done more easily in bash than in python and vice versa.

+9
Dec 15 '10 at 23:09
source share

Well, when you write using bash, you can directly use all the possible tools on the command line for your script. With any other language, you first need to execute this command and get the result, etc. A simple script that (for example) gets a list of processes, goes through grep and gets some result will be much more complicated in other languages. Thus, bash is still a good tool for writing fast things.

+6
May 20 '10 at 8:23 a.m.
source share

Many startup scripts are usually written as shell scripts, and there seems to be no tendency to move away from them.

Shell scripts are great for starting other processes and gluing their inputs / outputs together. This is an area where other scripting languages ​​are not so strong.

Also: even if languages ​​such as Perl, Python, and Ruby are becoming more and more ubiquitous, the only language you practically guarantee on every unix-like system is the bourne shell ("although this is not necessary in bash form).

+4
May 20 '10 at 8:26 a.m.
source share

Besides what others said, I would like to point out, in my opinion, the main reason for learning Bash: it is an (almost) standard Linux shell .

Other scripting languages ​​are certainly useful and perhaps much more powerful, but you will deal with you when you have a terminal in front of you ... Bash.

The ability to control I / O, pipes and processes, analyze and use variables, and perform at least some evaluation of the cycle and state if you want to control a Linux system.

+4
May 20 '10 at 16:11
source share

Bash is not only a scripting language, but also a shell.

Job Control Languages ​​and Shells
Main article: Shell script

The main class of scripting languages ​​grew out of the automation of control work, which relates to controlling the behavior of a program system. (In this sense, you could think of shells as descendants of the IBM JCL or the Job Management Language that was used for this purpose.) Many of these languages, translators are duplicated in command line interpreters, such as the Unix shell or MS-DOS COMMAND. Com. Others, such as because AppleScript suggests using English commands to build scripts. This combined with the Mac OS X Cocoa framework allows the user to create entire applications using AppleScript and Cocoa.

Python, Ruby, and Perl are great, but more general tools; in some cases (embedded devices or other minimalist systems) they can be considered bloated or in other situations they can pose a security risk (for environments where very high security is required, and try to eliminate any unnecessary package).

In some cases, shell scripts will work much better due to tighter integration with the OS.

Furthermore, bash is a production proven for many administrative tasks, and it’s easy to find scripts that will cover very complex scripts (exceptions) and handle them gracefully.

+3
May 20 '10 at 8:44
source share

Easier, probably not. In many cases, I prefer perl to bash scripts. However, bash has one advantage, especially on Linux systems: all this ensures that it is installed. And if it is not, its pretty much compatible father (sh) will be, because almost all system scripts are written for sh. Even perl is not ubiquitous, and it all happens somewhere.

+2
May 20 '10 at 8:27 a.m.
source share

If you make a lot of GUI files, you will probably only meet bash when you do some tuning on your own machine. Various hacks and stuff. If you use the command line to work, bash is simply indispensable. In fact, the command line requires bash or some other familiarity with the shell.

I get miles by recognizing bash when I want to quickly move around my hard drive. I wrote a navigation / menu interface that allows you to quickly and easily switch to different folders and files. Writing in bash was simple and straightforward. And there are many easily accessible and free things that will show you how.

Also, bash training is great for understanding how Unix and some of the core products really work - and how far we have come with tools like Python.

+2
May 20 '10 at 13:44
source share

Bash (and the original derivatives of Bourne sh and myriad) is, from one point of view, an incredibly high-level language. Where many languages ​​use simple primitives, shell primitives are entire programs.

So that this is not the best language for expressing your tasks, this does not mean that he is dead, dying or even dying.

+1
May 20 '10 at 8:27
source share

You can get real bournesh on top of freshmeat (for example, on Mac OS X / bin / sh is not true bournesh).

How to define a traditional Bourne shell? ... A simple check for a often undocumented but characteristic feature: you can use circumflex ^ (caret) as a replacement for | (Trumpet).

see: http://www.in-ulm.de/~mascheck/bourne/

+1
May 20 '10 at 16:03
source share

In my experience, Perl comes across something like 99% of any need that a shell script might require. As a bonus, you can write code that runs on Windows without Cygwin. If I don’t have Perl installed in the Windows window, I want to target, I can use PAR :: Packer or PerlApp to create an executable file. Python, Ruby, and others should work just as well.

However, shell scripts are not so complicated - at least everything you need to script in the shell is not so complicated. You can do what you need with a fairly low level of knowledge.

Learn how to read and set variables. How to create and call functions. How to download other files. Learn how flow control works.

And most importantly, learn to read the man page. This may sound funny, but I'm 100% serious - don't worry about cutting every detail of the shell scripts into your brain, instead learn how to quickly and efficiently find what you need to know on the manual page. If you often use shell scripts, the relevant information will naturally enter your brain.

So yes, the basic shell is worth exploring.

+1
May 20 '10 at 16:18
source share

I am a perl guy, but the number of bash (or ksh) functions that I use and create on a daily basis is quite significant. For anything, I will write a perl script, but for navigating through the directory structure and, in particular, for managing environment variables, bash / ksh / ... are needed.

Again, especially for environment variables, nothing beats the shell, and quite a few programs use environment variables. In Perl, I need to write an alias or bash function that calls a Perl script that writes out a temporary bash script that then gets the source after Perl exits to make changes to the same environment from which I run.

I did this, especially for weightlifting on variable paths. But there is no way to do this only in Perl (or python or ruby ​​... or C-code, for that matter).

+1
May 20 '10 at 19:46
source share

As mentioned, GNU tools are great and easiest to use in a shell. It is especially nice if your data is already in a linear or tabular form in plain text. As an example, the other day I managed to create a script to create the XHTML word cloud of any text file in lines 8 of the Bourne Shell, which is even less powerful (but more widely supported) than Bash.

+1
Jun 30 '10 at 8:02
source share

What I don't understand is why people say bash when they mean any compatible bourne-shell .

When writing shell scripts: always try to use constructs that also work in older bourne shell interpreters. This will save you a lot of trouble.

And yes, today there are many possibilities for shell scripts, because the shell always exists in all unixes, regardless of perl, python, csh, zsh, ksh (maybe?), Etc. Most of the time, they add only added convenience or a different syntax for constructs such as loops and tests. Some of them have improved redirection features.

In most cases, I would say that a conventional Bourne rocket works equally well.

Typical trap: if ! test $x -eq $y if ! test $x -eq $y works as expected in bash, which has a smarter built-in if statement, but the right one if test ! $x -eq $y if test ! $x -eq $y should work in all environments.

+1
Jul 08 2018-10-10T00:
source share



All Articles