Tag Info

Hot answers tagged

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

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


3

I think that .sh.version has existed ever since the first version of ATT ksh 93. It isn't available in pdksh or mksh. Since ${.sh.version} is a syntax error in shells other than ksh93, wrap the test for it in a subshell. _sh_version=$(echo "${.sh.version}") 2>/dev/null case $_sh_version in '') echo "This isn't ATT ksh93";; … esac KSH_VERSION ...


3

There are three cases: var might have been initially unset, empty, or non-empty. In the first two cases, it's set to /temp; in the last case it's left alone. Another way to do the same thing is : "${var:=/temp}" I prefer that one because the chain of assignments is clearer, but it's a matter of aesthetics. Under normal settings, this is equivalent to ...


3

It's a compact idiom. You can use it inline where commands are executed: cmd "${arg1}" "${arg2}" "${arg3:-42}" or at the start of the program (or as first statement in functions) to assert the parameter interface (using the : command): : "${1:?} ${2:?}" or check and assign it to the semantically named variables on which you operate in the program or ...


3

It’s more streamlined; you can do something in one simple command rather than two (for example, [[-z $var]] && var=/temp) or a complex command like if [[-z $var ]] then var=/temp fi Also, it doesn’t require that the conditional value be assigned to a variable.  For example, #!/bin/sh prog="$1" # Do sanity checks & input validation here. ...


3

KSH_VERSION was not implemented in ksh93 before version 93t. It will be set in mksh, pdksh, lksh. So for checking the version of ksh, we can try these steps: Checking KSH_VERSION to detect mksh, pdksh, lksh If first step fails, try a feature that's different between ksh93 and ksh88/86 (Let David Korn show us). With these in mind, I will go with: case ...


3

You should not be using exit inside your for loops - this causes the script to exit, and is why you're only getting one result. You should be using continue, which will stop the current loop from going on, but will go to the next element in the for loop. Swap both your exit statements to continue and you should find very different behavior, more in line ...


3

I'm going to make a first-stage stab at this. Someone else will hopefully improve. Before executing your script, the shell will open a file-descriptor to the file. Usually this is assigned at fd 255. At any rate, if there's an open fd, then lsof can find it. So we use lsof -p $$ and get the highest-file-descriptor's filename. lsof won't work with every ...


2

awk provides pattern matching awk '/ELF*.executable/ { ... }' EDIT: in your case: find /opt -type f | xargs ls -let | awk 'BEGIN { OFS="\t" } /ELF*.executable/ { sprintf("file \"%s\"", $10) | getline type; print type,$1,$3,$4 }' | tr ":" "\t"


2

If your OS supports FUSE, you can use SSHFS to mount a remote directory to a local one. Otherwise, assuming your shell is bash, you can still do it like this: program <(ssh b 'cat /path/to/file') But this only works if your program only wants to read from the file on machine b.


2

Perhaps like this: sftp username@server <<EOT cd $path get ubpbilp* get cust.cmp* get bunc.cmp* quit EOT as sftp doesn't support mget.


1

Awk could do this easily if the output was in decimal, but it can't parse hexadecimal numbers (at least standard awk can't, some versions such as GNU awk can). You can use bc to do the conversion. This works on all POSIX systems. { echo "ibase=16"; cat input.txt; } | bc | awk 'NR==1 {origin = $0-1} $0!=origin+NR {print "Out-of-sequence number at line", ...


1

Here is one option: while read x; do echo $((16#$x)); done <yourfile | awk 's && $1!=s+1{exit(1)}{s=$1}' This shell command will produce an exit status of 1 (on fail) and 0 (on success). This command can be used e.g. in an if-clause like the following to produce the desired output: if while read x; do echo $((16#$x)); done < yourfile | awk ...


1

As others have remarked, you need to pass the get commands as input to sftp. You can do it with a here document. Also, note that sftp doesn't have a mget command. sftp "username@server:$path" <<'EOF' get ubpbilp* ./ get cust.cmp* ./ get bunc.cmp* ./ EOF SFTP isn't very convenient to script. If the server allows scp, use it. If you just want to copy ...


1

There are heuristics that can help you, but there is no fully reliable way. Otheus shows how to use file descriptors. That's a nice heuristic, which works in most cases. However there are edge cases where it fails, and there's no way to detect failures. Example: take the following script. #!/bin/sh set lsof -p$$ | sed 's/[0-9][0-9]*//' Make two copies ...


1

grep command have -r option it searchs text recursive over directory example below: grep -r "5|20150507" ./ | awk -F ':' {'print $2'}


1

You can use piped I/O from a command in awk (at least gawk, I haven't tested this on Solaris): find . -type f | xargs ls -l | awk 'BEGIN { OFS="\t" } { command=sprintf("file \"%s\"", $9); command | getline type; close(command); print type, $3, $4 }' | tr ":" "\t" If your find supports it you can simplify this with find . -type f -ls | awk ... There's a ...


1

I like cuonglm's approach of probing the shell's capabilities to determine its version based on what is known to be different between versions. However, the check for $ERRNO could be fooled. While I was writing a script for ksh, I noticed that the -a option of ksh's built-in whence command appears to not be supported in older versions of ksh. This ...


1

mksh maintainer here ;-) though this is not the usual “support forum”… In mksh, both interactive edit (not ESC+v (vi) / ^Xe (emacs) though) is limited to single lines, as is history, as it is terminated internally by the newline character. This has always been so, even in pdksh. Apparently, pdksh in Debian was patched somehow, I see it has the ^J newline ...


1

It is not "overcheck" - it is "default value", in other word if value is not set (or empty) use default value.


1

To search backward in your ksh command history, Ctrl-R in emacs mode ought to work, even if you're running an old version such as ksh88. It is not an incremental character-by-character search like in bash. You have to type Ctrl-R, then the string you want to search for, then Enter.


1

I have a workaround that seems to work. I created a separate ksh script that does all the db2 commands (establish a connection, then creates a table and grants permissions on it), and run that with any arguments needed. This means that it's running a set of commands in one su instance, despite using the -c part, so as to run it inside the script ...



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