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 weird string "@(#)" is actually used by the ancient SCCS version control system. Specifically, the what command would look through a file (binary or text) and find ASCII-Nul-terminated strings that started with "@(#)", and print that string out. That allowed you to embed printable ASCII version numbers in ".o" files and ultimately executables, so you ...


7

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


6

Try "./a.sh" when trying to execute it. It needs to know where the file is at. The './' tells it to look in the current directory.


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


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


4

If you don't background the tail command in your script, the shell will wait for it to exit, which will never happen. If you have other work to do, after which you want to kill the tail command, you can tail -f logfile & tailpid=$! ...do some other stuff... kill $tailpid ...carry on...


4

Try: #!/bin/bash id touch script-run-user.file sudo -u appuser 'ksh' <<EOF # add list of cmds to execute id touch appuser.file EOF Edit: Just as an update, check out Here Documents. EOF = "End Of File", the name is arbitrary.


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

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


4

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


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

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.


3

Not sure what you mean. Possibly with GNU grep: grep -Ero '(\\x[[:xdigit:]]{2})+' . To match strings of the format \xNN (the 4 characters backslash, x and two hexadecimal digits)


3

You can use the literal newline in PS1: PS1="$_timedhms > [USERNAME]MACHINE:${PWD#$HOME/} $ " or using $'\n' with ksh93: PS1="$_timedhms$'\n' [USERNAME]MACHINE:${PWD#$HOME/} $ "


3

This is not a syntax error for actual ksh syntax, it's syntax error for invalid command name which only catched at run time. When you run it, you will get command not found error. If you add: echo foo ehco foo2 if [ 1 -lt 0 ] ...


3

When you type a command, the shell looks up the command from a list of directories, as specified by the PATH variable. The current directory is not in PATH by default (for security reason), so the shell can not find your script. Using ./, meaning the current directory, so the shell knows where is your script.


3

If you're using a sh-compatible shell (like bash), that > prompt is called the "secondary prompt". It's set by the value of the PS2 variable, just like PS1 sets the normal prompt. You should be able to change it to # pretty easily: PS2='# ' You might want to put that into your ~/.bashrc (or whatever the equivalent is for whatever shell you're using).



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