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 ...


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

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.


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

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

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 ...


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} ...


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

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

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

Assuming that you are talking about console programs, and not progress dialogs in a GUI, the effect of overwriting the last line can be achieved with STDOUT and with just about any console. Most consoles support the "carriage return" character, which brings the current output position to the beginning of the current line, without otherwise altering what is ...


3

They use a console handling library like ncurses or use \b or \r escape sequences to do it More info: https://stackoverflow.com/questions/656504/how-to-overwrite-stdout-in-c https://stackoverflow.com/questions/517127/how-do-i-write-output-in-same-place-on-the-console https://stackoverflow.com/questions/465348/how-can-i-print-over-the-current-line-in-a-...


3

This is a very common question for any programming language, and the answer is surprisingly simple. Do what makes sense and makes the program better. Here are qualities that make a program/script better in no particular order: Ease of understanding Ease of maintenance Ease of extension Ability to be tested and debugged Portability Performance If ...


3

do you agree this is a good way of solving the problem? No. When you parse a shell command line, you work your way through the string, and so at any point you should already know whether you are inside quotes. Either because your parsing state machine is in some "between quotes" state, or because your recursive descent parser is in some "parse quoted string"...


3

This is not strictly an answer to you question, but if you want to start a new project, do you really want to copy all the history, branches, etc from the old project? If you just want a working template, say to get the structure of the project as a starting point, but it should have no other ties to the original, then clone the repo but delete the .git ...


3

Your certainly on the right track. For my projects I use: "check" to unit test all method (including as many code paths as I can - have time for). This runs super fast and gives me confidence that the parts of my application are doing what I expect. "Valgrind" to check the memory usage of the final application while running system/regression tests. This is ...


3

First reason: UWIN was not originally open source; Second reason: Uwin executables requires a running service to be installed on the target OS, while cygwin uses a pure dynamic library.


3

If you don't want that inherited environment variables are used as initial values, it is important to initialize the variables you are using (or unset them explicitly). An environment variable named "runCommand" will not influence your first snipset, it will be used in your second one. (That may be a feature, but you have to be aware of it). And you need ...


3

This sounds similar to how CGI worked in the earliest days of the internet. Each web request would launch a process to return the contents of the page. The performance eventually became poor and an alternate solution was found. One solution was called FastCGI. With a FastCGI interface, the process could be left open and loaded. New requests were fed to ...


2

A few tips. Local variables are declare local and initializated to null if strings or 0 if number if you develop in bash use bash. (+=, for arg;do done;...) Use always builtin [[ notnumber ]] or (( number )) never [,test,( or any other function() { local runCommand= local -i index=0 #you don't need the extra parameters here for strings; do ...


2

Some things I like to do to keep my scripts maintainable: use functions Instead of putting everything into one giant script file create several small functions Example: main() { # put logic here } main "$@" # pass commandline args to main function within functions, declare variables with the local modifier main() { local result (($1 > 10) &...


2

I am big fan of stupid tools. I want my hammer to hammer and my screwdrivers to screw. If I use grep, I want to find something, but I don't want it to be able to e-mail me with the results. If I wanted that, I could pipe it to some other tool that would do that. Likewise, you have a Java program that can do anything imaginable. Why exactly wouldn't it ...


2

The extra options supported by Win32 MoveFileEx function provide a good overview and explanation of the similarities and differences between renaming and moving. When moving files across volumes, a copy (followed by delete) is performed. From the perspective of an ordinary computer user, when one wants to perform a rename, one would expect the computer to ...


2

How about storing the commands in an actual executable file? The end user creates a bash/python/ruby script, and all they supply to your configuration is the name of the script to execute. This allows the script to be arbitrarily simple or complex, and also lets the end user use any scripting language supported by their own environment. Finally, they will ...


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