Tag Info

Hot answers tagged

10

A few pieces of documentation will help to explain this. From the POSIX standards document for the shell: The following variables shall affect the execution of the shell: PS1: Each time an interactive shell is ready to read a command, the value of this variable shall be subjected to parameter expansion and written to standard error. ... ...


8

The problem with that style is that the two forms aren't equivalent. When you use: if command; then foo else bar fi then either foo or bar will be called, never both. When using both && and ||, both paths can be taken: $ [[ -d / ]] && { > echo "Path 1 taken" > false > } || { > echo "Path 2 taken" > } Path 1 taken ...


7

i=0 while true; do a[$i]=foo i=$((i+1)) printf "\r%d " $i done This simple script shows on my systems (Gnu/Linux and Solaris): ksh88 limits the size to 2^12-1 (4095). (subscript out of range ). Some older releases like the one on HP-UX limit the size to 1023. ksh93 limits the size of a array to 2^22-1 (4194303), your mileage may vary. bash ...


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


6

POSIX and Hyphens According to the POSIX standard, a function name must be a valid name and a name can consist of: 3.231 Name In the shell command language, a word consisting solely of underscores, digits, and alphabetics from the portable character set. The first character of a name is not a digit. A hyphen is not listed among the characters ...


6

From http://www.manpagez.com/man/1/ksh/: <>word Open file word for reading and writing as standard out- put. <&digit The standard input is duplicated from file descriptor digit (see dup(2)). Similarly for the standard output using >&digit. <&- ...


6

If your system supports /dev/fd/n: tar cvf /dev/fd/3 ./foo 3>&1 > foo.out 2>foo.err | squish > foo.tar.S Which with AT&T implementations of ksh (or bash or zsh) you could write using process substitution: tar cvf >(squish > foo.tar.S) ./foo > foo.out 2>foo.err That's doing exactly the same thing except that this time, ...


6

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


5

Try: $ awk '1;/PPP/{exit}' file AAA BBB JJJ OOO 345 211 BBB OOO OOO PPP


5

You have to use a named pipe for that. First create one in the folder: mkfifo foo.pipe Then use that command: tar cvf foo.pipe ./foo >foo.out 2>foo.err & cat foo.pipe >foo.tar Notice: the cat-part, can now also be gzip or whatever, that can read from a pipe: tar cvf foo.pipe ./foo >foo.out 2>foo.err & gzip -c foo.pipe ...


5

tar cjf <your-name-in-specific-path> <your-directory-path> c: Create j: Use bzip2 for compression f: Save it to given file name NOTE: If your tar version doesn't have these options you can follow the below instruction: tar cf <your-name-in-specific-path> <your-directory-path> gzip ...


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

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

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

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


4

The easiest way by far is to use the same shell on both systems. Just because one shell is preinstalled (there's no such thing as a “default shell” for scripts, the shell is whatever the shebang line says) doesn't mean that you can't install others. You can install bash on AIX (from the toolbox, for example), or ksh93 for Linux (with your distribution's ...


4

stty and older versions of who am i will issue error messages when they're not connected to a tty device. stty checks stdin (fd 0); I don't know what file descriptor who checks. To avoid getting those error messages, the usual workaround has been to use the -t option of test (more commonly known as [) to check if the shell is connected to a tty. if [ -t 0 ] ...


4

The \ character escapes the following (special) character. In this case, it escapes the $, which we usually use to dereference a variable. When the shell evaluates a variable assignment, it first expands the right-hand-side of the expression. Without the \ before $PWD, the shell expands $PWD and assigns the result to PS1. However, with the \, the shell ...


4

When a user invokes sudo -l it lists what sudo will allow them to do, so you could have a script ran as root that bumps through /etc/passwd and sudo's to each user, invoke the sudo -l, directing the output to /tmp/${USER}_sudo_i_can_do.txt But if you don't have root access, you won't be able to do what you want to do; the list of permissions is readable ...


4

awk is a better tool for comparing columns of files. See, for example, the answer to: compare two columns of different files and print if it matches -- there are similar answers out there for printing lines for matching columns. Since you want to print lines that don't match, we can create an awk command that prints the lines in file2 for which column 2 has ...


4

Not only does ksh use sfio but it uses its own custom memory allocator. Nevertheless, my guess is sfio makes the difference in this case. I just tried to run your example under strace and can see that ksh calls read/write ~200 times (65 KB blocks) while sed does it ~3400 times (4 KB blocks). With sed -u my laptop almost melted, reads are done per byte and ...


4

The reason for this is ( has a different meaning. From the bash manpage: (list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command ...


4

ksh does not have pushd, popd as built-in. But it has an implementation for you. Try: . /usr/share/ksh/functions/pushd or: . /usr/share/ksh/functions/popd Then you can use pushd and popd. To make it permanent, you can source those files directly in your .kshrc or add them to FPATH environment variable.


4

sed "s/.\{$(($RANDOM%${#a}))\}/&$b/" <<< $a where: $RANDOM pseudo-random value from 0 to $RAND_MAX (usually 0x7fff == 32767) ${#a} length of target string $((...%...)) outputs the resedue from dividing .{n} match first n characters of input string s/.../&$b/ substitutes pattern match by themselves + $b


4

sed -n -e '/[A-Z][A-Z][A-Z]/p' prints the lines that match that regexp. Here, you'd want: sed -n 's/.*\([[:upper:]]\{3\}\).*/\1/p' That is, you want to substitute a sequence of any characters (as many as possible) followed by 3 uppercase letters (captured in \1 with \(...\)) followed by a sequence of any characters with the captured letters and print ...


4

Note that the <<- word here-doc form requires that only tab characters can appear before word. You can't use spaces, must be tabs. ref: http://www2.research.att.com/sw/download/man/man1/ksh.html#Input/Output


4

For "real" ksh releases (i.e. AT&T based), I use this command: strings /bin/ksh | grep Version | tail -2 Here are various output I get: Original ksh: @(#)Version M-11/16/88i dtksh; @(#)Version 12/28/93 Version not defined Modern ksh93: @(#)$Id: Version AJM 93u+ 2012-08-01 $ For pdksh/msh ksh clones and modern AT&T ksh versions too, here ...


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

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


3

slm's answer here hasn't taken into account that you asked about the Korn shell, not about the Bourne Again shell. The (93) Korn shell has no built-in expr command, so when using expr in Korn shell scripts you are using an external expr command. This is not a problem per se. After all, it's how one did things with the Bourne shell, which also had no expr ...



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