The Korn shell (ksh) is a shell with advanced scripting features, commonly found on commercial unices and some BSD systems but rarely used on Linux.
64
votes
1answer
10k views
Difference between nohup, disown and &
What is the difference between
$ nohup foo
and
$ foo &
and
$ foo &
$ disown
22
votes
5answers
1k views
When do you use brace expansion?
I understand what brace expansion is, but I don't know how best to use it.
When do you use it?
Please teach me some convenient and remarkable examples if you have your own tip.
19
votes
2answers
648 views
Why escape trivial characters in shell script?
I just opened a legacy shell script (written in old ksh88 on Solaris) and found the following repeated all throughout the code:
[ -f $myfile ] && \rm -f $myfile
The escaping backslash ...
18
votes
3answers
2k views
using single or double bracket - bash
I'm confused with using single or double bracket. look at code:
dir="/home/mazimi/VirtualBox VMs"
if [[ -d ${dir} ]]; then
echo "yep"
fi
It works perfectly although the string contains space. ...
13
votes
2answers
439 views
How can one time out a root shell after a certain period of time?
Is there a way to 'time out' a root shell (for example, in gnome-terminal) so that after a certain amount of time not issuing any commands, the shell exits?
I'm searching for a solution that works in ...
8
votes
7answers
485 views
determine shell in script during runtime
As per my knowledge, to determine the current shell we use echo $0 in the shell. Rather I want my script to check in which shell it is running. So, I tried to print $0 in the script and it returns the ...
7
votes
4answers
966 views
Executing user defined function in a find -exec call
I'm on Solaris 10 and I have tested the following with ksh (88), bash (3.00) and zsh (4.2.1).
The following code doesn't yield any result:
function foo {
echo "Hello World"
}
find somedir -exec ...
7
votes
3answers
2k views
Getting size with du of files only
How can I get the size of all files and all files in its subdirectories using the du command.
I am trying the following command to get the size of all files (and files in subdirectories)
find . ...
6
votes
5answers
853 views
How to avoid a useless use of cat when parsing a file?
I have a file containing a list of directories. For instance
/foo/bar/dir1
/foo/bar/dir2
/foo/bar/dir3
I want to create all these directories. Here's what I did:
for dir in $(cat myfile); do ...
6
votes
2answers
381 views
When is it useful to use “grep — SOMEPATTERN”?
So under Linux/AIX where ever.., when is it useful to use:
grep -- SOMEPATTERN
the Question is the "--" part. When is it usefull to use?
6
votes
1answer
329 views
Why does a=0; let a++ return exit code 1?
Try it:
$ a=0
$ let a++
$ echo $?
1 # Did the world just go mad?
$ echo $a
1 # Yes, it did.
$ let a++
$ echo $?
0 # We have normality.
$ echo $a
2
Contrast with this:
$ b=0
$ let b+=1
$ echo $?
0
...
6
votes
2answers
636 views
Using sed to color the output from a command on solaris
I have a ksh script that must work on both linux and solaris. I'm trying to color the output of specific commands. It works on linux (specifically RHEL6), but not on solaris (SunOS 5.10).
Command ...
6
votes
1answer
165 views
Ksh features incorporated into Zsh?
I have been a user of Bash for some time. I want to learn at least one other shell now, so I have been picking up the Korn Shell. However, it doesn't look like Ksh has had major updates for almost ...
5
votes
5answers
336 views
What is the difference between $* and $@?
Consider the following code:
foo () {
echo $*
}
bar () {
echo $@
}
foo 1 2 3 4
bar 1 2 3 4
It outputs:
1 2 3 4
1 2 3 4
I am using Ksh88, but I am interested in other common ...
5
votes
5answers
348 views
Shell programming, avoiding tempfiles
I often write KSH shell scripts that follow the same pattern:
(1) retrieve output from one or more command
(2) format it using grep|cut|awk|sed and print it to the screen or to a file
In order to ...
5
votes
4answers
2k views
How to have separate command history for different sessions for the same user?
I face this issue on some of Unix servers. When I open different session for same user, command history is shared by all the session. This creates issues if by mistake I press Ctrl-P or up arrow and ...
5
votes
1answer
110 views
Find all users who have more than N processes and echo them in shell
I'm writing a script in ksh. I need to find all users who have more than N processes and echo them in the shell. N is read from ksh.
I know that I should use ps -elf, but how do I parse it, find ...
5
votes
1answer
818 views
Reason for ksh obsoleting -eq
The latest version of ksh obsoletes using -eq within [[ ]] blocks, prefering (( )) instead. Why is this? I can't find any documentation on the advantages of (( )) over [[ ]] anywhere, and I find that ...
5
votes
2answers
868 views
Adding Ctrl-L as “clear” hotkey in OpenBSD's ksh
I've been looking for a way to use Ctrl-L to clear the monitor instead of typing clear in each time. I found nothing except a patch which didn't work for me. How do I add hotkeys/shortcuts to my ...
5
votes
1answer
343 views
Poor Man's GNU Parallel implemented in ksh?
I'd like to use the feature of GNU parallel where it can execute the command and the list it's fed in parallel and spit it out after it's all done, however, I don't want to install GNU parallel across ...
5
votes
2answers
1k views
Shell script execution on multiple servers
Is it possible to run one part of the ksh/sh script on one server then ssh to another server and continue with the rest of the script? or is there a way around? Anyone had experience with this ?
here ...
5
votes
1answer
735 views
Korn Shell: End, pgup, pgdown, and delete key not working
So basically my End, Pageup/Pagedown, and Delete key are not working in ksh93. I'm running FreeBSD by the way.
My arrow keys are working, and so is my home key.
Those keys work when I put this in my ...
4
votes
4answers
439 views
A unix command to truncate each line of a file
I have a CSV file and I want to truncate it from the third semicolon. For example, if I have this file:
1;foo;bar;baz;x;y;z
2;foo;bar;baz;x;y;z
3;foo;bar;baz;x;y;z
I want to get the following ...
4
votes
2answers
2k views
How to get a clean environment in a ksh shell?
I need to get rid of all the environment variables in a Ksh shell. I can fork a new instance, but it will inevitably source some init files (as far as I know .profile, .kshrc). Is there a way to ...
4
votes
4answers
65 views
linux + how to ignore (filter) file with space
Sometimes a file name is created from a couple of names and space between each name as the following:
$ touch "A B C"
$ ls
A B C <-- one file but has three words ! ...
4
votes
3answers
1k views
How to get subshell's PID in Korn Shell (equivalent of $BASHPID)
In bash you have this handy variable: $BASHPID wich always returns the currently running subshell's PID. How can I get a subshell's PID in ksh? For example see the code below:
#!/usr/bin/ksh93
echo ...
4
votes
6answers
348 views
What should I do with a remote AIX machine if I accidentally “chmod 644”-ed the “/usr/bin/ksh”?
Now the machine asks for password...The ssh was configured with key auth.. The problem is that ksh doesn't has executable permission anymore.. :\ What can I do remotely? Any tips?
The root's shell ...
4
votes
1answer
282 views
Ksh loses data after piping 16K bytes
I recently found that ksh may lose some data after printing more than 16K bytes to the stdout if it is blocked for a couple of seconds.
This test.sh script prints out 257*64 (16448) bytes:
...
4
votes
5answers
300 views
Last command in ksh
In bash I can use !! to indicate the last command. In ksh I think you can use r to do something similar, but it seems to fail in one of my common patterns: typing sudo [last command].
> cp foo ...
4
votes
1answer
789 views
What is the most correct way to pass an array to a function?
Consider I have a very large array $large_list, is there a way to write a function that will take the array as an argument? For example:
echo_idx_array () {
arr="$1"
idx="$2"
echo ...
3
votes
2answers
273 views
Skip first 3 byte of a file
I am using AIX 6.1 ksh shell
I want to use one liner to do something like this
cat A_FILE | skip-first-3-bytes-of-the-file
I want to skip the first 3 bytes of the first line, is there a way to ...
3
votes
3answers
1k views
How to use cut command to get the first and last elements of a row?
I've asked almost the same question already, but this time, I want to retrieve the X latest elements of a row of a CSV file. For example, with an input file as this one:
1;foo;bar;baz;x;y;z
...
3
votes
1answer
89 views
Output of `java -version` not matched by grep or awk
On my Linux machine, it isn't clear to me why if I do the following then I don't get only the version string ("1.5.0_32").
# java -version | grep version | awk '{print $NF}'
java version "1.5.0_32"
...
3
votes
2answers
431 views
Use a variable reference “inside” another variable
I'm sure it is relatively simple, I just don't know how to do it.
#!/usr/bin/ksh
set `iostat`
myvar=6
I want to something like echo $($myvar) which i want interpreted as $($myvar) -> $(6) -> value
...
3
votes
3answers
813 views
What are the significant differences between different shells? [duplicate]
Possible Duplicate:
What are the fundamental differences between the mainstream *NIX shells?
If I write a shell script in bash, will it run in ksh as well?
if not, what are the ...
3
votes
4answers
3k views
Match regex in ksh
I am looking to do something like this in KSH:
if (( $var = (foo|bar)[0-9]*$ )); then
print "variable matched regex"
fi
Is it possible at all?
For the record I'm using Ksh Version M-11/16/88i ...
3
votes
4answers
513 views
Formatting the output: Underlining
I wrote the following function in ksh that prints its first argument to the screen and underlines it with the appropriate ammount of - character:
print_underlined () {
word=$1
echo $word
...
3
votes
4answers
166 views
Piping output to text file within a for loop
I'm trying to do the following within a for loop:
Find files that satisfy a condition
Echo the name of the files to a log file.
Gzip the file.
I can get the script to find the files and echo their ...
3
votes
3answers
600 views
Solaris: find the day of last Monday,Tuesday,…Sunday by means of shell script
I'm trying desperatly to find a bash or ksh routine that allows me to find for example the previous Monday,Tuesday,Wednesday,... preceding today's date. Additonal it has to work on plain vanilla ...
3
votes
2answers
367 views
ANDed conditional using regexp and variables
I want to test whether a line, read in from a file, has a specific beginning AND an ending containing a word held in a variable. Here's some code:
The input file is:
line one
#; line two
#; line ...
3
votes
2answers
1k views
How to catch optioned and non optioned arguments correctly?
I want to write a shell script which will take some arguments with some options and print that arguments. Suppose the name of that script is abc.ksh. Usage of that script is -
./abc.ksh -[a ...
3
votes
2answers
191 views
Error when subtracting two negative numbers in ksh: “assignment requires lvalue”
I am trying to debug someone else's script:
The code line is:
y=$((${oldvalue[$x]}-${newvalue[$x]}))
y gets calculated fine as long as both sides are positive numbers. However, I have a ...
3
votes
3answers
262 views
What does exit do in an if block in a shell script?
I have a question regarding unix shell script.
Say if you do exit 1 in inner if: will it exit or will it still execute the outer if? The following is a dummy example.
if [ "$PASSWORD" == ...
3
votes
2answers
125 views
Execute command on shared account login
At work our team uses a shared account "appadmin" to administer our application. At login, each one of us sources an "aliasrc" file containing his or her preferences. (aliases, display, prompt, ...).
...
3
votes
3answers
1k views
How do I run a script n times at same time and how do I simulate a semaphore?
I have a text file, inside of this file is a number, and I have a script.sh in ksh.
The script reads the file and gets the number, then increases the number by 1 and overwrites the new number in the ...
3
votes
1answer
430 views
Unset Read Only Variables
Is it possible to unset or change a readonly variable in Korn Shell? I am using AIX 5.
3
votes
3answers
2k views
Using nohup on Solaris 10
I need to transfer a large number of files over SFTP (only between Solaris servers) which takes a very long time. I cannot keep my PC on for this duration. I tried:
nohup sftp server1
While the ...
3
votes
1answer
84 views
Trapping dot (.) file not found errors in KSH
In ksh88, I can source a file using the "dot" command, like
. /my/file/source.ksh
However, if source.ksh doesn't exist, I want to trap the error.
So I tried this:
#!/bin/ksh
trap "echo 'Source ...
3
votes
1answer
180 views
Why does sendmail work differently in different shells?
The following code works when I directly run it in bash shell:
SUBJECT="SUBJECT-"`date`;
MAIL_FROM="abc@site.com";
MAIL_TO="abc@site.com";
MAIL_CC="abc@site.com";
MAIL_FILE="/path/of/html/body.html";
...
3
votes
3answers
1k views
Get the number of files that match a pattern in a directory and delete the oldest one
I'd like to do the following:
Get the number of files in a given directory that match a given pattern, for example:
ExtractBackup_{date}.tar.gz
If that number is 2 or higher, delete the oldest file ...

