Hot answers tagged

28

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


7

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

Using sed: variable1="$(< inputfile sed -n '3s/ *//p')" variable1="$([...])": runs the command [...] in a subshell and assigns its output to the variable $variable < inputfile: redirects the content of inputfile to sed's stdin -n: suppresses output sed command breakdown: 3: asserts to perform the following command only on the 3rd line of input ...


7

The standard (POSIX sh and utilities) canonical legible ways would be: string comparison: if [ "$a" != 0 ] || [ "$b" != 100 ]; then... decimal integer comparison (0100 is 100, whether leading blanks are ignored or not depend on the implementation though). if [ "$a" -ne 0 ] || [ "$b" -ne 100 ]; then... integer comparison (0x64, 0144 are 100 (POSIX mode ...


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


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

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

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


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

You can stop the process with ctrl-z. Then do whatever you want in the terminal. To continue the process use fg. Or from another terminal, use: kill -19 <pid> It sends SIGSTOP (signal number 19) to the process. This is not possible to catch for the process. To continue the process use: kill -18 <pid> This time it's SIGCONT that brings the ...


5

First read the desired line into a variable (line 3 in the example): var=$(sed -n '3p' file1.txt) The sed command prints (p) the 3rd line of the file. The strip the leading spaces using parameter substitution: echo "${var#"${var%%[![:space:]]*}"}" The inner substitution means to remove everyting except the leading spaces. The outer substitution remove ...


5

There is no generic solution as there are countless ways a script could be using to call other scripts. You can do a grep which may work for your scripts but not in general. Which scripts does this call? $(find / -executable -name "*.sh" -print0 | shuf -z -n 1) If you're able to actually run these scripts you could trace them in two ways. set -x will ...


5

Here is a pure-awk solution: $ awk 'FNR==NR{a[$1]=$2; next} {print a[$1]}' bar.txt foo.txt good bad okay large good bad How it works The script reads in bar.txt first and saves its information in array a. It then reads foo.txt and prints out the corresponding value. FNR==NR{a[$1]=$2; next} FNR is the number of lines read from the current file and NR ...


4

tail -1 file1.txt > variable1 writes to the file variable1. Use command substitution (bash.info 3.5.4, POSIX sh) instead: variable1="$(tail -1 file1.txt)" Btw my version of tail from GNU in cygwin doesn't have the -1 option. Instead, I use sed: # EREGEX: Replace all whitespace at beginning of line # NOTE: BSD sed uses a different flag to enable ...


4

This topic is interest, so I test the benchmark in 3 ways: sed '1d' d.txt > tmp.txt tail -n +2 d.txt > tmp.txt sed -i '1d' d.txt Note that target d.txt is 5.4GB file Get the result : run 1 : sed '1d' d.txt > r1.txt 14s run 2 : tail -n +2 d.txt > r2.txt 20s run 3 : sed -i '1d' d.txt 88s Conclusion : It seems below be the quickest ...


4

If your concern is about aliases, just do: [[ $(unalias -- "$cmd"; type -- "$cmd") = *builtin ]] ($(...) create a subshell environment, so unalias is only in effect there). If you're also concerned about functions, also run command unset -f -- "$cmd" before type.


4

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


4

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


4

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


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

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


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


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


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

@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

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

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

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



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