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

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


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


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

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


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

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

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


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

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

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

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

#! /bin/zsh - for dir (/home/DABA_BACKUP/*) rm -f $dir/*(Nom[61,-1]) For the zsh-ignorant ;-): for var (list) cmd: short version of the for var in list; do cmd; done loop (reminiscent of perl syntax). $dir: zsh variables don't need quoted like they do in other shells as zsh has explicit split and glob operators so doesn't do implicit split+glob upon ...


3

With ksh/zsh/bash: IFS=' ' read -r variable < <(tail -n 1 file) read strips leading and trailing space characters if space is found in IFS (which it is by default along with tab and newline). You can also do: while IFS=' ' read -r variable <&3; do something with "$variable" done 3< file To process the file line by line (though that's ...


3

Asusming all scripts live in the same directory, they don't have tabs or newlines in their names, and you have the list of the "interesting" ones in a file scripts.txt, one per line, and also assuming your shell can do <(...) process substitutions: #! /bin/sh while read -r s; do fgrep -o -w -f <(fgrep -v -w "$s" scripts.txt) "$s" /dev/null | \ ...


3

awk '$0 == $0+0{ summ += $0 next} { if(summ) format="%06d\n%s\n" else format="%s%s\n" printf format, summ, $0 summ=""} END { if(summ) printf "%06d\n", summ}' ...


3

You're invoking ksh. The kind of substitution you are wanting to do works only since ksh '93. Is there a chance you are using an older version? Run ksh and check for the existence of KSH_VERSION. If it doesn't exist or is before '93, it's too old.


3

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


3

Even though ksh has more advanced feature than other shells, it's still at its heart a language to automate simple task and write small scripts, not a language geared towards writing huge applications. You can't expect to have all the features of a typical language designed for large applications, let alone all the features of C++. I don't think you can ...


3

For ksh93, you have (at least) a couple of choices associative arrays envir=Dev foo["$envir"]=bar echo "${foo["$envir"]}" namerefs nameref var=${envir}foo var=bar echo "$var" For ksh88, you may be stuck with eval: envir=Dev name="${envir}foo" eval "$name=bar" eval "echo \$$name"



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