Questions tagged [ksh]

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.

Filter by
Sorted by
Tagged with
128 votes
4 answers
51k views

Why does parameter expansion with spaces without quotes work inside double brackets "[[" but not inside single brackets "["?

I'm confused with using single or double brackets. Look at this code: dir="/home/mazimi/VirtualBox VMs" if [[ -d ${dir} ]]; then echo "yep" fi It works perfectly although the string contains a ...
Majid Azimi's user avatar
  • 3,088
146 votes
11 answers
54k 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 ...
rahmu's user avatar
  • 20k
45 votes
4 answers
161k 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
Brandon Kreisel's user avatar
116 votes
4 answers
40k views

How do you use the command coproc in various shells?

Can someone provide a couple of examples on how to use coproc?
slm's user avatar
  • 369k
45 votes
4 answers
40k 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 ...
rahmu's user avatar
  • 20k
19 votes
1 answer
2k views

How to use associative arrays safely inside arithmetic expressions?

A few Bourne-like shells support associative arrays: ksh93 (since 1993), zsh (since 1998), bash (since 2009), though with some differences in behaviour between the 3. A common use is for counting ...
Stéphane Chazelas's user avatar
10 votes
2 answers
39k views

Bash/Korn shell script edited on Windows throws error '...^M: not found'

I wrote a Bash script in Windows using Notepad++. cxStopAllServicesOnSERVER.sh #!/usr/bin/bash cd "/some/path" echo "Hi. Logs can be found at "`pwd`"/cxStartStopLogger.log" echo "["`date`"]*** ...
Kent Pawar's user avatar
  • 1,276
8 votes
2 answers
2k views

Circular name references in bash shell function, but not in ksh

I'm writing a set of shell functions that I want to have working in both Bash and KornShell93, but with Bash I'm running into a "circular name reference" warning. This is the essence of the problem: ...
Kusalananda's user avatar
  • 331k
45 votes
2 answers
22k views

Are there problems with hyphens in functions, aliases, and executables?

In my testing (in Bash and Z Shell), I saw no problems with defining functions or aliases or executable shell scripts which have hyphens in the name, but I'm not confident that this will be okay in ...
iconoclast's user avatar
  • 9,177
37 votes
3 answers
177k views

Remove all files created before a certain date

I have a directory containing a high number of files (like logs for every day of the year). I would like to remove all files created before let's say 22/11. How can I achieve that ? Must I use find ...
user1058398's user avatar
  • 4,158
10 votes
2 answers
24k views

How do I capture the return status and use tee at the same time in korn shell? [duplicate]

Consider Source code: 1. Parent.sh #!/usr/bin/ksh # No tee ksh Child.sh; exit_status=$?; echo "Exit status: ${exit_status}" # Using tee ksh Child.sh | tee -a log.txt; exit_status=$?; echo "Exit ...
Kent Pawar's user avatar
  • 1,276
61 votes
4 answers
11k views

Is the shell ksh93 dead?

On 2013-01-10 Glenn Fowler posted this to the ast-users mailing list: As has been pointed out several times on the AST and UWIN lists, AT&T gives very little support to OpenSouce software, ...
mikeserv's user avatar
  • 58.2k
21 votes
1 answer
4k 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 ...
l0b0's user avatar
  • 51.1k
14 votes
7 answers
27k views

How can I safely get the version of ksh?

How can I safely get the version of ksh from within a ksh script? I have seen the following solutions: ksh --version echo ${.sh.version} echo $KSH_VERSION And given the right circumstances, each of ...
Sildoreth's user avatar
  • 1,864
8 votes
3 answers
18k 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 "${arr[...
rahmu's user avatar
  • 20k
59 votes
7 answers
19k 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.
16 votes
5 answers
9k views

Find directories that do not contain subdirectories

I'm writing script is ksh. Need to find all directory names directly under the current directory which contain only files, not subdirectories. I know that I could use ls -alR and recursively parse ...
Nat Kup's user avatar
  • 163
10 votes
4 answers
4k 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 do ...
rahmu's user avatar
  • 20k
10 votes
2 answers
24k views

How do I escape a sub-directory name with an ampersand in it?

Running a kornshell and trying to traverse a directory tree. Want to cd to a sub-directory named as follows: -3ab_&_-3dc.img My question is HOW do I need to escape the ampersand in this name? I'...
RCinICT's user avatar
  • 101
8 votes
3 answers
28k views

Can ksh on AIX be configured to use the tab key for filename completion?

I have some AIX 7 servers that are restricted to what software I can install and wonder if I can get ksh to use the tab key to complete filenames at the shell promot. The man pages are sparse for ksh ...
bmike's user avatar
  • 302
7 votes
3 answers
446 views

Is there any reason to avoid sourcing the shell's configuration file?

I recently read an answer here that included the following advice: However, as a rule, ⚠️you should not source ~/.zshrc. Depending on what's in your dotfiles, this can lead to all sorts of problems. ...
terdon's user avatar
  • 241k
6 votes
3 answers
684 views

Is "Arithmetic Expansion" the expected action on vars inside `[[` tests?

This script: a=2 [[ "$a" -eq 2 ]] && echo yes1 [[ $a -eq 2 ]] && echo yes2 [[ a -eq 2 ]] && echo yes3 [[ "a" -eq 2 ]] && echo yes4 Does not work in dash (and ...
user avatar
5 votes
3 answers
399 views

What shells support "value substitution" like MirBSD Korn Shell?

On Android (which uses mksh, MirBSD Korn Shell), there's a special syntax of string substitution (called "value substitution"): ${|commands} Instead of collecting output of the commands (as ...
iBug's user avatar
  • 3,498
2 votes
1 answer
350 views

How can I redirect all output of a script to a file and replace passwords?

I'm running a ksh script on AIX 7.2. In debug mode I want to redirect all the script does to a brkpt-file. The script also makes logins to another application and therefore uses a password (let's say &...
rudi's user avatar
  • 23
83 votes
6 answers
89k 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 . -...
Shardul Upadhyay's user avatar
44 votes
1 answer
8k views

What's wrong with var=$(</dev/stdin) to read stdin into a variable?

We've seen a few posts here lately that use this: var=$(</dev/stdin) in an attempt to read the shell's standard input into a variable. That is however not the correct way to do it on Linux-based ...
Stéphane Chazelas's user avatar
23 votes
5 answers
42k views

Is there a way to sum up the size of files listed?

This is the command I am using to list some files: find . -name \*.extract.sys -size +1000000c -exec ls -lrt {} \; -rw-r--r-- 1 qa1wrk15 test 1265190 Sep 29 01:14 ./var/can/projs/ar/rep/...
munish's user avatar
  • 7,957
17 votes
3 answers
98k views

while read loop

I have a file like one below var 3 2014 string var1 4 2011 string4 var2 6 1999 string2 var3 1 2016 string6 Then i have this while read loop to compare one of the columns ...
kickass13's user avatar
  • 173
15 votes
2 answers
4k 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 ...
LanceBaynes's user avatar
11 votes
3 answers
27k views

Using Diff on a specific column in a file

Will it be possible to use diff on a specific columns in a file? file1 Something 123 item1 Something 456 item2 Something 768 item3 Something 353 item4 file2 Another 123 stuff1 Another 193 ...
kickass13's user avatar
  • 113
10 votes
7 answers
70k views

Sum the values in a column except the header

I have a file as given below -------------------------------------------------------------- Name_Customer Item_Purchased Item_Amount Credit ---------------------------------------------------------...
Ram's user avatar
  • 383
10 votes
1 answer
502 views

How is ksh93 so fast?

So, in general, I tend to look to sed for text processing - especially for large files - and usually avoid doing those sorts of things in the shell itself. I think, though, that may change. I was ...
mikeserv's user avatar
  • 58.2k
8 votes
5 answers
16k 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 "...
Patkos Csaba's user avatar
  • 2,520
7 votes
2 answers
13k 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 ...
rahmu's user avatar
  • 20k
7 votes
1 answer
662 views

Where is $_ defined by POSIX?

In the comments to the accepted answer to this Unix & Linux StackExchange question, it is stated that $_ is "one of only some 7 special parameters defined by POSIX". I can't find that definition ...
jrw32982's user avatar
  • 713
6 votes
5 answers
25k views

Next day of the user entered date - date +1 day

I am new to scripting. I need a script in the AIX environment to get the next days date based on the date entered by the user. After hard time browsing for this, I got a piece of code which does the ...
cm60's user avatar
  • 61
6 votes
3 answers
71k views

-ksh: revenue_ext.ksh: not found [No such file or directory]

I am getting the same not found [No such file or directory] error when trying to execute a ksh script. Read tips about the PATH and running the script with a ./ in the posts here and here and tried ...
user68112's user avatar
6 votes
1 answer
2k views

typeset in ksh93 not working as expected

I thought typeset was ksh's local, but this fails in ksh93 although it works in all my other typeset-supporting shells (bash, yash, zsh, pdksh) #!/bin/ksh -ex foo(){ typeset a b a=0; b=1 ...
Petr Skocik's user avatar
  • 28.8k
4 votes
6 answers
7k views

Check in ksh if a directory is empty

How do I check if a directory is empty or not in ksh on AIX? If files exist I need to remove files only.
Aravind's user avatar
  • 1,589
4 votes
3 answers
38k views

How to iterate through a comma-separated list and execute a command for each entry

I have been give a command which outputs a comma-separated list for autosys jobs in the variable $all_jobs: box=box-of-jobs;all_jobs=$(jobscout -box $box | egrep "^\w+" | tr '\n' ',' | sed s/.$//); ...
Ian's user avatar
  • 53
3 votes
3 answers
3k views

How to compare two float values in ksh?

To compare float values below command is working fine in bash, But not working in ksh on AIX. if (( $(bc <<< " 5.3 >= 5.3") ));then echo "hi";else echo "bye";fi`, I had tried in so many ...
Sriram P's user avatar
2 votes
3 answers
1k views

regexp in ksh for extensions tgz, tar.tgz

I'm trying to get a regexp (in ksh) to identify files with only the following extensions: tgz, tar.gz, TGZ and TAR.GZ. I tried several but can't get them to work. I'm using this regexp to select only ...
user463674's user avatar
2 votes
2 answers
719 views

Meaning of \ before environment variable

In ksh on an old Solaris box I used: export PS1="$PWD $" to set the prompt to the current directory. It works great until you cd elsewhere, then you see that it's not evaluating PWD each time. I ...
user avatar
1 vote
1 answer
961 views

How to organize files in main directory into smaller amount of files in sub-directories [duplicate]

I have a directory of 150 files that I wanted split into sub-directories of 25 files each: 1-25 into dir1, 26-50 into dir2, and so on. How can I accomplish this? dir1/fre_4_g2_c3654214.h3 dir1/...
e1v1s's user avatar
  • 161
1 vote
3 answers
3k views

How to set the command(s) that get run before every single prompt?

I used to know this but I forgot: I want to execute a command or a couple of commands before every single prompt. When I am in our PROD box, I want a big fat reminder above every prompt that says * ...
amphibient's user avatar
  • 12.4k
1 vote
2 answers
4k views

put tab before every output line on AIX/ksh

0:root@SERVER:/root # echo "something" | sed -e 's/^/\t/g' tsomething 0:root@SERVER:/root # AIX/ksh .. why doesn't it works? I just want to put a "tab" before every line!
LanceBaynes's user avatar
1 vote
3 answers
376 views

[[ 0*10%300 ]] works on AIX 6.1 but not on AIX 7.1 (ksh)

I have a ksh93 script that I'm migrating from AIX 6.1 to AIX 7.1 It's failing on 7.1 but works fine on 6.1. Here's a snippet of the important parts. integer f_count=0 . . . . . . . . . ...
Scavenger's user avatar
  • 145
1 vote
4 answers
25k views

Cronjob doesn't execute shell script, but when I am executing it standalone it works. Why?

Shell Type: >echo $SHELL /bin/ksh Cronjob : 15 * * * * /bin/ksh /wls_domains/resMGT/logs/bea/wlr3queuetransaction.sh > /wls_domains/resMGT/logs/bea/data/script.log The script is as below: #...
neal mukherjee's user avatar
0 votes
5 answers
16k views

How to grep an xml block in an xml file using a keyword in ksh

I have a file Sample.xml which contains a lot of services inside it and the structure looks like this Notes: I cannot use any XML parser tool as I don't have permission, read only My xmllint ...
Philip Morris's user avatar
82 votes
15 answers
97k views

How to color diff output?

I wanted to format the Unix files conditionally, I am currently working on diff command and wanted to know if it is possible to format the text of the diff command output. Example: Matched ...
Aman's user avatar
  • 1,191