New answers tagged

3

MacPorts has dash, which is what Debian uses. For that and other choices, read Is there a minimally POSIX.2 compliant shell?, in which the answers point out that the standard is vague in places.


2

if [ "1024" == "$((32*32))" ]; then echo "The test worked" else echo "The test failed" fi This ought to work; if your shell does not use $(( )) for arithmetic, the strings will not match. You can also shorthand it with: [ "1024" == "$((32*32))" ] || echo "I can't math!"


2

The key to shell performance is to minimise the number of expensive system calls, in particular fork() and exec(). Don't use grep or sed inside a shell loop. Never use a pipeline with both; in most cases, it can be reduced to just sed or awk. If it gets complicated, use a language that can parse regular expressions, and do loops, like awk or perl. On the ...


1

for shell in for shell in $(sed '1d' /etc/shells); do # or use your own list of shells echo "$shell -" time $shell /path/to/script done


1

"declared" means that you've used declare or typeset to specify the type of the variable. This allows you to specify that the variable names an array, should always be treated as an integer, should be read-only, etc. This doesn't exist in POSIX shell, it's a bash extension. I think "defined" and "set" mean the same thing: a value has been given to the ...


3

Here's an awk script that stores the whole file in memory: awk '{line[NR]=$0} END {for (i=NR; i>=1; i--) print line[i]}' file Phrased as a shell function: tac () { awk '{line[NR]=$0} END {for (i=NR; i>=1; i--) print line[i]}' "$@"; }


1

Here's a clean clear-cut POSIX solution for on-disk files: #!/bin/sh function tac () { lines=$(wc -l < "$1") while [ $lines -gt 0 ] do head -n $lines "$1" | tail -n 1 lines=$((lines-1)) done } The main down-side is that it reads the file once for every line in the file. POSIX doesn't specify an upper limit for -n number, so large files ...


2

You can do this with a sed one-liner as follows, though it is certainly not readable for the "uninitiated": sed -n '1h;1!{x;H;};${g;p;}' file.txt Explanation: -n suppresses sed's default action of printing each line. 1h causes the first line to be stored in the hold space. 1!{...} applies this block of commands to all lines except for the first one. ...


1

The two main reasons to run a program directly without calling the shell are: Performance: Most programs that you would call from your C program are likely much smaller than the shell, which makes them start much more quickly. Environment control: Dealing with an additional layer of environment variables to deal with can be more complex to configure and ...


1

It's what it says on the tin: “Maximum length of command we could actually use” is the maximum possible command line length, given the limit on the platform where xargs is running and the space taken up by the environment. This value only depends on the platform configuration and the environment. “Size of command buffer we are actually using” is the size ...


-3

This is a well known pitfall in the definition of the tar command line syntax. The problem can be avoided by using my star instead. star uses a different much less error sensible CLI definition when you call it under the name star and it is still much safer than the traditional tar implementation as it will not overwrite plain files when called as tar.


0

Explanation: The f in cvf is a shortcut for the -f option, which expects the name of the target file to tar to as next parameter. The outcome of switching the order of parameters has already been described by @AndrewHenle and @StephenKitt.


4

You asked tar to archive the files file2 and total.tar in the archive called file1, which it attempted to do. Unfortunately that means that file1 was overwritten, all you can get from it now is file2: tar tvf file1 (don't add a z in there, you didn't specify it when creating the archive). The only way you'll recover file1 is from backups.



Top 50 recent answers are included