Hot answers tagged

29

It is not possible to give a real answer to this question, but the form of a comment is not sufficient. So I think it may be a good idea to collect points to a editable answer... Two years ago, David and Glenn have been layed off by AT&T - I guess both are now over 65. Half a year later, they have been hired by Google and Glenn confirmed me that their ...


10

Yes and no. The official AT&T Korn Shell may be pining for the fjords, but there are two actively developed clones. There's pdksh, the public domain clone of the Korn shell, but that hasn't been updated in 16 years, it seems. However, OpenBSD uses pdksh as the default /bin/sh and they update it fairly frequently. NetBSD's default install has pdksh as ...


10

Ksh93 does a lot to avoid forks. I have no idea how it knows how to handle the first case, as a truss shows that it only calls one write(2) call with the final result. It may be that David scans the command in macro.c and knows that he may handle "echo" internally. What I can say is that I rewrote the parser and the interpreter of the "Bourne Shell" last ...


8

Ctrl+R works with ksh in emacs mode (ksh -o emacs or set -o emacs within ksh), and it was most probably the first shell to support it. Only it's not as interactive as in zsh or bash or tcsh's i-search-back widget. In ksh (both ksh88 and ksh93), you type Ctrl+RtextReturn. And Ctrl+RReturn to search again with the same text. In vi mode, you can use ? to ...


7

Neither bash nor ksh can perform floating point arithmetic (ksh93 supports that if I remember correctly). I recommend to switch to zsh or run external tool like bc: $ CPU_IDLE=98.67 $ echo "$CPU_IDLE" $ CPU_USAGE=$( bc <<< "100 - $CPU_IDLE" ) $ echo "$CPU_USAGE" 1.33


7

NO tldr: github.com/att/ast and github.com/att/uwin On Jan 19-20, 2016 the following (1|2) messages were posted to the ast-users mailing-list: (and I consider the dgk has some patches comment especially encouraging) Wed, Jan 20 2016; From Glenn Fowler: Thanks Lefty for all the work getting this up and running. I know dgk has some patches in ...


6

I don't think you can, and I don't think it actually makes any difference. unset a a=x echo "${a[0]-not array}" x That does the same thing in either of ksh93 and bash. It looks like possibly all variables are arrays in those shells, or at least any regular variable which has not been assigned special attributes, but I didn't check much of that. The ...


6

There are different implementations for a standard command, on a system. Like in Solaris 10 and earlier, you have /bin/sh is the old Bourne shell and /usr/xpg4/bin/sh Is POSIX compliant shell. Or on OSX, you have BSD sed when calling sed and GNU sed when calling gsed. You can chose what implementation you want to use in your script. So it's easier to change ...


5

With sort (it will also sort the output): sort -u file With awk (it doesn't sort, just filter out the duplicates, like the desired output): awk '!a[$0]++' file With perl (same principle as the awk one): perl -ne 'print unless $a{$_}++' file


5

From man ksh: An arithmetic expression uses the same syntax, precedence, and associativity of expression as the C language. All the C language operators that apply to floating point quantities can be used... Variables can be referenced by name within an arithmetic expression without using the parameter expansion syntax. When a variable is referenced, its ...


5

So you effectively want just the middle part of declare -p without the junk around it? You could write a macro such as: readonly VARTYPE='{ read __; case "`declare -p "$__"`" in "declare -a"*) echo array;; "declare -A"*) echo hash;; "declare -- "*) echo scalar;; esac; } <<<' so that ...


5

In zsh zsh% a=(1 2 3) s=1 zsh% [[ ${(t)a} == *array* ]] && echo array array zsh% [[ ${(t)s} == *array* ]] && echo array zsh%


4

Try this as an alternative solution, which will invoke cat only when it finds a file find /home/peter/databases -type f -name 'cells.txt' -exec cat {} + (If your version of find does not understand the trailing +, replace it with the two characters \;)


4

Most likely, this is erroneous. The form -size +0 is legal for the find standard utility (and means a non-empty file), but not for test (a.k.a. [). It seems that the script author reused it without keeping proper context. If this is ksh, it should have the following check: –s file True, if file exists and has size greater than zero. Alternatively, ...


4

The Korn shell does support history searches using CtrlR, at least since ksh93 (and perhaps even ksh88), but they don't work quite like bash. First you need to enable Emacs mode: set -o emacs (This enables other niceties, such as arrow keys working by default.) Now if you press CtrlR, the shell will print ^R; type your search, hit Enter, and the shell ...


3

This will check for the correct format (YYYY-MM-DD) in bash (with built-in regex match): if [[ $1 =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] then echo "Date $1 is in valid format (YYYY-MM-DD)" else echo "Date $1 is in an invalid format (not YYYY-MM-DD)" fi Run: ./script.sh 2015-12-10 Output: Date 2015-12-10 is in valid format (YYYY-MM-DD) It doesn't check ...


3

bash only implements a subset of the features of ksh93. While it implements most of the features of ksh88, for some of them it does it differently and not all features are enabled by default. For instance, aliases in bash are not expanded when non-interactive, the ksh extended globs are not enabled by default, bash has no print builtin, can't define arrays ...


3

The script isn't really doing anything useful. The extension is irrelevant and the shebang line is ignored if you run with bash explicitly. You can get the exact same thing as what your script does if you just run: bash script.ksh The issue is the differences in syntax. That's what you will need to fix, otherwise, just run the scripts with bash and be ...


3

GNU tar's --index-file option works well: tar cvf - ./foo 2>foo.err --index-file=foo.out | squish > foo.tar.S


3

Your script can be slightly modified to only process the states you need: netstat -ant | awk '/ESTABLISHED|LISTEN|CLOSE_WAIT/ {print $6}' | \ sort | uniq -c | sort -n A further step would be to everything with awk, e.g. : netstat -ant | awk ' /ESTABLISHED|LISTEN|CLOSE_WAIT/ {count[$6]++} END { for(s in count) { printf("%12s : %6d\n", s, count[s]...


3

zsh, pdksh (though not recent versions of mksh derived from that), yash, the Bourne shell behave like dash. Only bash, ksh88, ksh93 and mksh behave otherwise. The POSIX spec is not clear on what should be the correct behaviour, but there's nothing in there that says that the shell is allowed to override the default handler for the SIGINT (or other) signal. ...


3

This is very well documented, e.g. in bash: [[ expression ]] [...] Word splitting and pathname expansion are not performed on the words between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are ...


3

To test variable var, with b=("${!var[@]}") c="${#b[@]}" It is possible to test if there are more than one array index: [[ $c > 1 ]] && echo "Var is an array" If the first index value is not zero: [[ ${b[0]} -eq 0 ]] && echo "Var is an array" ## should be 1 for zsh. The only hard confusion is when there is only one index ...


3

"Yes and no. The official AT&T Korn Shell may be pining for the fjords, but there are two actively developed clones." Unfortunately pdksh and mksh are ksh88 near workalikes (clone is too strong). I have yet to find an actual ksh93 workalike (bash and zsh don't come all that close). Personally I hope that the transition to Google will speed up the ...


3

If your not familiar with "vi" or "emacs" prompt commands the best would be to use the fc shell built-in command look at the "fc" help into the man sh-posix manpage. Use the mouse to copy paste the commands. The HP-UX shell is /usr/bin/sh the "POSIX shell" which command prompt is close to the korn shell, by default it is set to the vi command mode "Esc" ...


3

@cuonglm's comment illustrates how you can do this with sed: sed -e 's/.*\(Apple\)/\1/' input.txt You might also consider using good old grep: grep -o 'Apple.*' input.txt Caveat 1 - I don't have any solaris handy, so it might be that the solaris grep doesn't have the -o option. YMMV. Caveat 2 - If your input file has lines that don't contain any ...


3

You can force interactive session using -tt switch in your script: ssh -tt user@hostB ./scripts/test.ksh Otherwise the TTY on the hostB will not get allocated and you will get such errors.


3

The passage you quote does not mean what you say it means. Patterns Matching a Single Character (…) An ordinary character is a pattern that shall match itself. (…) If any character (ordinary, shell special, or pattern special) is quoted, that pattern shall match the character itself. All of this applies only to characters that stand for ...


3

Red Hat provides "ksh-93", which does not have a bind command. It provides something called KEYBD, which is triggered via a special trap function. According to the manual page, that can be used to modify the Emacs Editing Mode. The example at [Korn Shell] Fixing Home, End, pgup, pgdown, and delete keys is a useful starting point for showing how to make ^...


3

My advice is keep things simple. Don't write a whole script when there is a ready-made tool that already does what you want. du is the tool for reporting on disk usage, and find is the tool for finding files. Use them together. find dirname* -mindepth 1 -maxdepth 1 -type d -exec du -hs {} \; -maxdepth and -mindepth are GNU extensions; to handle this ...



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