ksh

How to implement singleton in shell script

泄露秘密 提交于 2019-12-02 09:00:01
In kornshell, `basename $0` gives me the name of the current script. How would I exploit $$ or $PPID to implement the singleton pattern of only having one script named `basename $0` executed on this server by any user? ps -ef|grep `basename $0` This will show me all processes which are running that have the name of the currently running script. I need a script which can abort when a thread which is not $$ is running the script named `basename $0`. To provide a race-free mutex, flock is your friend. If you aren't on Linux -- where it's provided by util-linux -- a portable version is available.

Cannot debug simple ksh programme

爷,独闯天下 提交于 2019-12-02 07:42:31
问题 I wrote this sample KornShell (ksh) code but it is getting bad substitution error during the if clause. while ((i < $halflen)) do if [[${strtochk:i:i}==${strtochk:j:j}]];then i++ j-- else ispalindrome = false fi done Please help. NB: I am using ksh88 , not ksh93 . 回答1: shell syntax is very whitespace sensitive: [[ is acually the name of a command, it's not just syntax, so there must be a space following it. The last argument of [[ must be ]] , so it needs to be preceded by a space. [[ works

pass wildcard to cut command in shell script and store it in a variable

你说的曾经没有我的故事 提交于 2019-12-02 04:48:48
I am new to shell, I have a case where I am trying to evaluate a particular column unique values to check if they are valid in a shell script which will be invoked later. From my searches I think cut along with sort & unique is good to do it So my attempt is file=/filepath/*vendor.csv file_categories = `cut -d, -f1 $file |sort |unique` $file should hold file which has vendor in its filename but even after using command substitution (`) the $file is not getting replaced with the correct filename , it just places what is present in file Another example for what I am attempting is a=/stage

Scoping problems in different shell languages?

倖福魔咒の 提交于 2019-12-02 03:52:34
It appears that pdksh and mksh has the scoping implementation I expected. For example: readonly x='global' f() { local x readonly x='f' echo $x } g() { local x readonly x='g' echo $x } echo $x f g echo $x pdksh and mksh produce my expected result: global f g global And Bash fails: line 5: local: x: readonly variable Dash and Ksh93 failed my expect, too. (I've changed local to typeset in Ksh93's test.) This seems confusing. UPDATE: I've edited the question. The question before is not stated in a clear way. Bash and Dash don't fail if the global variable is not read only. Korn (ksh93) doesn't

Why does the following IF condition in ksh always evaluate to true?

寵の児 提交于 2019-12-02 03:25:31
问题 Consider, below code works as expected: if [[ $SOME_VARIABLE = "TRUE" ]]; then echo "Only echoed when \$SOME_VARIABLE stores string \"TRUE\"." fi But when I remove the space surrounding the equality operator it always evaluates to 0 exit status (At least that's what I assume it must be returning as it is taken as true): if [[ $SOME_VARIABLE="TRUE" ]]; then echo "Always true." fi UPDATE: Just to confirm whether the issue lies with the equality operator or not: #!usr/bin/ksh SOME_VARIABLE=FALSE

Why does the following IF condition in ksh always evaluate to true?

人走茶凉 提交于 2019-12-02 02:48:34
Consider, below code works as expected: if [[ $SOME_VARIABLE = "TRUE" ]]; then echo "Only echoed when \$SOME_VARIABLE stores string \"TRUE\"." fi But when I remove the space surrounding the equality operator it always evaluates to 0 exit status (At least that's what I assume it must be returning as it is taken as true): if [[ $SOME_VARIABLE="TRUE" ]]; then echo "Always true." fi UPDATE: Just to confirm whether the issue lies with the equality operator or not: #!usr/bin/ksh SOME_VARIABLE=FALSE if [[ $SOME_VARIABLE == "TRUE" ]]; then echo "Only echoed when \$SOME_VARIABLE stores string \"TRUE\".

Unix parsing pipe delimited format string in ksh

妖精的绣舞 提交于 2019-12-02 01:45:32
I am writing ksh script to parse a pipe delimited string export dummy="abc" echo "123|456|789" | awk '{split($0,output,"|"); print output[3] output[2] output[1]}' above code seems to work , but I am not able to assign value of output[3] to dummy. Is there a way to do such parsing, but I want to assign the parsing result in a variable within ksh space i.e. dummy (in above sample)? The shell can do it: line="123|456|789" IFS='|' read a b c <<END $line END echo $c # => 789 You can't assign awk variables (i.e. output[3] ) to shell variables (i.e. dummy ), you can only assign the output of awk to a

Is it possible to increase the maximum number of characters that ksh variable accepts?

六眼飞鱼酱① 提交于 2019-12-01 19:51:37
This is a follow up question to What is the maximum number of characters that the ksh variable accepts? I checked my environment and it's allowing only #include <sys/limits.h> $ cpp << HERE | tail -1 > #include <limits.h> > ARG_MAX > HERE 1048576 Is there a way to increase this? Or any alternatives for while read line; do #parse logic done < $filename To handle really long lines? Based from the records I'm parsing it will not stop at 2M character lines. Environment Details : AIX $ KSH Version M-11/16/88f You could compile a Linux 3.7.x kernel, and edit its include/uapi/linux/limits.h file to

How to format the date in KornShell script to DD-MON-YYYY?

ε祈祈猫儿з 提交于 2019-12-01 19:16:05
How do I format a date in a KornShell (ksh) script to DD-MON-YYYY? I have tried the following: date '+%d-%h-%Y' It returns 04-Nov-2009 I need for the Nov to be NOV (all caps). Can this be done with the date utility? This is what finally worked on unix(solaris). date '+%d-%h-%Y' | tr [:lower:] [:upper:] returned: 04-NOV-2009 Mark Rushakoff The ^ character forces uppercase in the GNU coreutils date (at least, it does in version 6.9.92.4 of coreutils): $ date '+%d-%^h-%Y' 04-NOV-2009 Unfortunately, ^ is not POSIX standard for date , so you'll probably have to resort to a second command such as

KornShell Sort Array of Integers

巧了我就是萌 提交于 2019-12-01 13:20:06
Is there a command in KornShell (ksh) scripting to sort an array of integers? In this specific case, I am interested in simplicity over efficiency. For example if the variable $UNSORTED_ARR contained values "100911, 111228, 090822" and I wanted to store the result in $SORTED_ARR Is it actually an indexed array or a list in a string? Array: UNSORTED_ARR=(100911 111228 090822) SORTED_ARR=($(printf "%s\n" ${UNSORTED_ARR[@]} | sort -n)) String: UNSORTED_ARR="100911, 111228, 090822" SORTED_ARR=$(IFS=, printf "%s\n" ${UNSORTED_ARR[@]} | sort -n | sed ':a;$s/\n/,/g;N;ba') There are several other ways