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 only reference I can find to the special parameter $_ in POSIX is in the rationale section on Shell Variables. This excerpt implies that it was used by a variety of shells, but not in a standard way by all and was omitted intentionally: _ (Underscore.) While underscore is historical practice, its overloaded usage in the KornShell is confusing, ...


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

With awk: awk '{ print $(NF-1) }' NF is the number of fields -- all that happens here is that one is subtracted from the total field length to get the penultimate field. With perl: perl -lane 'print $F[-2]' An array containing the fields is created as @F (that's what -a does), and we get the value of the second last field (with index -2). Using sed ...


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

You're missing the do keyword: while ...; do ... done


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

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.


3

I put the below in a subshell so you don't change your current environment too drastically - they're all native shell builtins. (set -f; unset IFS ; set -- ${line} shift "$((${#}${2+-2}))" && printf ${2+%s}'\n' "$1") I edited this to disallow globbing - I added set -f according to advice offered in the comments below.


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


3

They are already in an array, the array of positional parameters $@ with individual elements accessed with $1, $2... (start at 1, $0 is the script name). Note that there are several implementations (and versions thereof) of ksh: AT&T ksh88 (as found in most commercial Unices), AT&T ksh93 (made open source in 2000, sometimes found as dtksh on ...


3

If you don't have ksh93 available, you can do it in standard ksh like this: function capitalize { typeset -u first first=${1%"${1#?}"} print "${first}${1#?}" } ${1#?} strips all characters matching "?" from the beginning of the first arg. i.e. strips off the first character ${var%pattern} strips off pattern from the end of var. If we use the ...


3

There is a way in bash 4.3+, which probably comes from ksh: echo_idx_array () # array index { local -n array=$1 # add nameref attribute local idx=$2 echo "${array[idx]}" } $ names=(one two three four) $ echo_idx_array names 2 three $ days=([monday]=eggs [tuesday]=bread [sunday]=jam) # associative array $ echo_idx_array days sunday jam ...


3

File descriptors belong to a process: the current process, that is. Immediately when you exit a process, the assignments have no effect. Even a subshell (or any child process) that inherit the file descriptors, have from the fork() call their own copies of the file descriptors (a file descriptor is just a number pointing to an IO resource in linux kernel). ...


3

There are ksh specific commands/options/shortcuts/features that won't work or work differently with zsh, and there are even more zsh specific things that would fail under ksh. If your goal is to write scripts, my advice would be to stick to POSIX features shared by both shells. zsh might miss some POSIX ones as compliance is not in its design objectives. ...


3

You need a do after the while: while test $count -ge 1 do ... done


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

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



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