Hot answers tagged

59

It's not particularly bad, but there are some caveats. how portable will your solution be? Will your chosen binary operate the same everywhere, output the results in the same format etc.? Will it output differently on settings of LANG etc.? how much extra load does this add on your process? Forking a binary results in a lot more load and requires more ...


37

It takes extreme care to guard against injection vulnerabilities once you've introduced a potential vector. It's in the forefront of your mind now, but later you may need the ability to select ttyUSB0-3, then that list will be used in other places so it will get factored out to follow the single responsibility principle, then a customer will have a ...


30

Shells have specialized features for working with files and getting data from one program into another (assuming that data is text). For those tasks, shell scripts can be less cumbersome than a scripting language like Python. Shell scripting also has the advantage that the commands you use are basically the same commands you'd use from the command line -- ...


25

REPL: This is a procedure that just loops, accepts one command at a time, executing it, and printing the result. The three steps at each iteration of the loop are: Calling read to read the characters that make up a textual expression from the keyboard input buffer, and construct a data structure to represent it, Calling eval to evaluate the expression--...


19

The existing answers are also valid, but there's one reason that no one has mentioned yet: because it WILL be there. Any given *nix install is done with some set of optional packages which may or may not be loaded, and not all systems will have Python or Perl or Ruby. But if the system is expected to have any interactive capability at all, it will have ...


16

In your specific case, where you want to invoke udevadm, I'd suspect you could pull in udev as a library and make the appropriate function calls as an alternative? e.g., you could take a look at what udevadm itself is doing when invoke in "info" mode: https://github.com/gentoo/eudev/blob/master/src/udev/udevadm-info.c and make equiv calls as to those ...


11

According to Wikipedia, the sh program reused the syntax from ALGOL 68. Stephen Bourne carried into this shell some aspects of the ALGOL 68C compiler that he had been working on at Cambridge University. Notably he reused portions of ALGOL 68's "if ~ then ~ elif ~ else ~ fi", "case ~ in ~ esac" and "for ~ while ~ do ~ od" (using done instead of od) clauses ...


11

According to http://slashdot.org/story/01/02/06/2030205/David-Korn-Tells-All (question 11), UWIN was not originally open source (though that appears to have changed in the 11 years since that interview was published). Not being open source would have been a significant barrier to widespread adoption, especially considering a functionally equivalent open ...


10

Convenience of syntax, mostly. In python, running a process requires subprocess.call(...); renaming a file shutil.move(), etc. Bash syntax is much more direct for those tasks. Yes, Python is a great language, but the explicit syntax to execute (for bash) simple tasks is going to get in the way of doing day-to-day work real fast.


9

Technically, it's correct to say that a shell is an instance of a REPL. However, it isn't a matter of program definition as it is one of common usage scenarios. Bash, for example, is written in C, but it could well have been written in Python. At that point, if you talk about program features and abilities, would it be correct to say that Bash is a shell ...


9

If you new to subversion I would suggestion skipping SVN and moving straight to Git (or Mercurial if you what). Git (and other distributed version control systems) are designed to work a lot better locally and for the most part have everything you would need from something like subversion. If you do go with git I would highly suggest you learn the command ...


9

A significant portion of your question is answered here: Why are scripting languages (e.g. Perl, Python, Ruby) not suitable as shell languages? Here's an excerpt from my answer to that question: There are a couple of differences that I can think of; just thoughtstreaming here, in no particular order: Python & Co. are designed to be good at ...


8

$ is the default prompt prefix for a non-escalated bourn or sh shell on most *nix systems. It signifies that the code exemplified should be executed using one of those associated shells. zsh and csh have % as a prefix. The fish shell uses >. Putting the prefix is simply a quick way to let the reader know what shell the command is destined for.


7

In bash, you can write mv img_{0..5} imgs/ which is expanded by the shell to mv img_0 img_1 img_2 img_3 img_4 img_5 imgs/ You can also use a character class: mv img_[0-5] imgs/ which would only work for those files that already exist and match the expression, while the former expands to all the possibilities even if the files do not exist. The curlies ...


7

Your question seemed to call for a forest answer, and the answers here seem like tree answers, so I thought I'd give you a forest answer. This is very rarely how C programs are written. It is always how shell scripts are written, and sometimes how Python, perl or Ruby programs are written. People typically write in C for easy use of system libraries and ...


6

What would I expect from a sane program (a shell script or not): I never ever have to alter the executable to just configure it. This is not an OS kernel. I can pass any setting using command line. This is a must for every piece of information that does not have a reasonable default. The only exception is a password which needs interactive input. Optionally,...


6

No. You find these "reversed" language constructs in many languages: { and }, /* and */ (* and *) :)


6

Possible Uses Fast Prototyping You could do this with Groovy or others, so that's not a very strong point, but as CRaSH gives you code-level access to the JVM and the processes it runs, it may come in handy to just keep a JVM running and experiment with small code snippets (for instance, using it as a REPL to implement solutions to StackOverflow questions)....


6

This is really hard to answer, because a ton of information is missing: How did you measure those sizes? Do they include libraries required to run the interpreter? Do they include runtimes? What Python implementation are you talking about? There are four production-ready implementations of Python in current use (PyPy, IronPython, Jython, CPython), plus ...


6

Your question is sort of like saying assembly is difficult to work with, so computers should use higher level languages instead. The ANSI format is the right level of abstraction for working with terminal hardware. You have to do the state tracking somewhere, and it's best to not do it in hardware that needs to be as cheap as possible. Even in modern ...


5

In shell languages of the Bourne family, the : command, which does nothing at all, is typically used in two situations: Placeholder for when something expects a mandatory command, e.g. while some_condtion do : done since do requires at least one command. Discarding the arguments, but performing side effects inside the argument list, e.g. : ${myvar=foo} ...


5

I'm not going to debug your code, there's not enough context to do this anyway, but I'm going to show you an idiom that you will probably find easier to use correctly. As a bonus, it will also be faster. Have a look at your loop body. You are allocating memory during each iteration and free it under certain circumstances depending on the overall control ...


4

It's never a good idea to have too many embedded if/else or loops, but in their own right, lots aren't inherently a bad thing. Consider splitting out the loops unto utility functions. For example, if you have: for stuff .. for stuff ... endfor endfor It would be much better read as findAllPricesForProducts( extractNamePriceTuple( data ) ) with ...


4

To answer your immediate concern, bash can do some range-based actions on existing files. $ ls sample1.txt sample2.txt sample3.txt sample4.txt samples $ mv sample[1-3].txt samples/ $ ls * sample4.txt samples: sample1.txt sample2.txt sample3.txt But there is a bigger trend at play here. Array programming tries to get away from explicit loops. It ...


4

Is there a way I can prove\disprove this? No, because Different operating systems do things differently. Different kinds of renames/moves can be conceptually different acts: Renaming/moving a file within the same directory, Renaming/moving a file from one folder/directory to another on the same logical device, Renaming/moving a file from one logical ...


4

Copying copies the working copy, any untracked files, and any local configuration along with the actual repository. Cloning doesn't, but it sets up the origin remote with a tracking branch. Use copying if you want to duplicate your repository so that all remote operations work the same. Use cloning if you want to create a subordinate repository to your ...


4

There is a simple reason. When you are typing you can only input a character at a time. Something that is processing typed text has to deal with what in effect is invalid markup, half a json blob or half an xml document. You can't display "error" until the typing has finished and then display the result. You have to have a format where partial documents ...


4

Context-free grammars are grammars where the left side of every production rule is a non terminal term (aka an abstract grammar construct) whereas the right side can be a mix of terminals (aka tokens that you find in the language) and non-terminals. Context-sensitive grammars are more tricky, since both the left and the right side of production rules can be ...


3

It sounds to me like you will simply be handing these files to another piece of software to actually read them in. If that's the case, use python or ruby or whatever easy to use high level language you have on hand because this program isn't actually IO intensive. Now, if you are actually reading the contents of files in yourself to process on your own then ...


Only top voted, non community-wiki answers of a minimum length are eligible