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

The grep with awk is redundant: hasys -display | awk '/Shutdown/ { printf "%s ", $1 }'


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

With GNU sort and GNU du (which it appears you have, since you state you are using du's -h option): du -sh -- * | sort -rh # Files and directories, or du -sh -- */ | sort -rh # Directories only The output looks something like this: 22G foo/ 21G bar/ 5.4G baz/ 2.1G qux/ 1021M wibble/ 4.0K wobble/


6

awk -Fy -v OFS=y '{gsub(",","u",$2); print}' file


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.


5

There are 2 redirects there. The last bit, 2>&1 is actually merging STDERR in with STDOUT. This looks to me like someone set this up to log output to the doit.log file but then wanted to disable it. Chaining redirects in this manner, basically negates the earlier ones, so that only the output, if there is any, will get directed to the last redirected ...


5

You could put the read and your case in a while loop and break out of it when the condition is satisfied: while : ; do echo "yes or no?" read ans case $ans in [yY]*) echo "yes" break ;; [nN]*) echo "no" break ;; *) echo "yes or no only" ;; esac done The while : ; do ... ...


4

Assuming all the filenames in the current directory follow that pattern, with GNU ls: ls -rv | awk -F_ '++n[$1]>2' | xargs rm You can omit the | xargs rm if you want to check what it would remove first. That assumes the part in {...} doesn't contain space, newline, single quote, double quote, backslash, tab or underscore characters. The key is the -v ...


4

There are 3 main implementations of ksh the original one from David Korn (AT&T ksh), with two main branches: ksh88 and ksh93 (and for ksh93, many version with new features added for each). pdksh, the public domain version (a free reimplementation of ksh88 with which it is mostly compatible) which is the base upon which is built the sh on some BSDs like ...


4

Like Lawrence has mentioned, you can use cp -v to enable "verbose" mode, which displays the files you copy. Something else that might be useful is cp -v > foo which will output the list of files to a file called foo. This is useful if you're going to copy a lot of files and you want to be able to review the list later.


4

You can do it in two steps: newdir="$(find . -type d | grep "${4:0:4}.*${4:4:2}.*${4:6:2}")" if [ -z "$newdir" ];then # error out here else cd "$newdir" fi ...


4

hasys -display | awk '/Shutdown/ {print $1}' | paste -sd ' ' - Contrary to @devnull's and @jasonwryan's, this one doesn't add a trailing space and terminates the line. paste -sd<sep> - is the generic idiom to join lines into a <sep> separated list.


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


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

Simply use $(()) for shell arithmetic: a=$(($a-1))


3

cp -v enables verbose mode which displays what's being copied.


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

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

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

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


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

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