ksh

Hi Dears

kindly I am new here ...I want to ask your help ...

I want to write AWK command when enter date like 20140129 plus 9 then it gives me 20140207 I wrote below but the result was wrong

#!/bin/ksh

echo "please input the data:"
read a


if echo $a|awk '{if (substr($1,5,1)=="5") {substr($1,6,2)++} else {print "wrong"}}'

Hello shaho87,

Welcome to forums, kindly use code tags for commands/codes/inputs which you are using in your posts, as per forum rules.
Following may help you in same, if you a GNU date in your system.

date --date="20140129 +9 day"

Thanks,
R. Singh

1 Like

Hello Ravinder,

kindly , thanks a lot but i want to write script not to use the date.

Hello Shaho87,

Could you please try following and let me know if this helps.

cat date_script.ksh
echo "Please enter a date in YYYYMMDD format:"
read DATE
echo "Please enter number of days which we want to take date from today's:"
read DAYS
date -d"$DATE +$DAYS day"

Following will be output after running the script.

./date_script.ksh
Please enter a date in YYYYMMDD format:
20140202
Please enter number of days which we want to take date from today's:
10
Wed Feb 12 00:00:00 EST 2014

Hope this helps.

Thanks,
R. Singh

1 Like

Hello Ravinder,

kindly,thanks a lot but i want to write a script do the operation manually i mean when we are type date the system directly know it but i want to build mine one thanks million for your helping.

Hello shaho87,

Could you please go through following thread I have given a solution for getting date 1 day ago for NON GNU, kindly take it as a start up and then try frm your end and if you feel any problems please do let us know, we ca assist you.

Thanks,
R. Singh

1 Like

The script that you provided reads a line of data provided by a user into a shell variable named a . It then uses awk to see if the 5th character in the 1st string of non-blank characters is a "5" (presumably the 1st digit in a two digit month is a "5"), and if it is it gets the 6th character from that string, adds 1 to it, and throws it away; otherwise, it prints the string "wrong". In either case, your script then gets a syntax error from the Korn shell because you have an if statement with no matching then nor fi .

Is this a homework assignment?

If it isn't, can you explain (in English) what needs to be done to a string in the format YYYYMMDD to add 9 days to it and end up with a valid date in the same format. If you can't explain what your script needs to do to update your user's input string, you would be much better off using the tools on your system that are designed to perform date arithmetic instead of reinventing the wheel. Even if you can explain the steps required, please explain why you want to reinvent the wheel rather than use system tools that have been written to correctly handle these types of calculations in normal years, in leap years, and (in some cases) even across the conversion from Julian to Gregorian calendars.

And, please tell us what operating system you're using. An operation like this can be performed with a 1988 vintage Korn shell, but some things would be easier with a 1993 or later version of the Korn shell. What version of the Korn shell is installed as /bin/ksh varies from system to system.

1 Like

Dears

kindly , thank you very much for both of you, I am very happy to see the prompt reply from you. today I registered in this website am sure it wont make me regret ...I am new in ksh scripting that is why I wanted to write script to add X number with date for example
20140129 +9 days the result will be 20140207 I saw it difficult because as it has been mentioned by Mr. Don i should consider normal year,leap year and we have 12 months but march is only 28 days i hope my comment is clear to you thanks again ...currently I am using software called admin ksh for windows because i have windows operating system.

---------- Post updated at 02:38 PM ---------- Previous update was at 02:28 PM ----------

Hello Ravinder,

after your posted link i found below i my required script but i dont understand it very well

cat script.ksh
echo $1 | awk -vs1="01" -vs2="03" -vs3="12" '{
                                                if(length($0)>8){
                                                                        print "Wrong Input!!";
                                                                        exit
                                                                }
                                                YEAR=substr($0,1,4);
                                                YEAR+0;
                                                MONTH=substr($0,5,2);
                                                MONTH+0;
                                                DATE=substr($0,7,2);
                                                DATE+0;
                                                split("31 29 31 30 31 30 31 31 30 31 30 31", W," ");
                                                if(DATE==s1 && MONTH==s1){
                                                                                YEAR=YEAR-1;
                                                                                MONTH=12;
                                                                                DATE=31;
                                                                                print YEAR MONTH DATE
                                                                         }
                                                else if(DATE==s1 && MONTH >= s1 && MONTH <= s3){
                                                                                MONTH=MONTH-1;
                                                                                W[2]=(YEAR%4!=0)?28:29;
                                                                                print YEAR MONTH W[MONTH]
                                                                                                }
                                                else {
                                                                                DATE=DATE-1;
                                                                                printf("%04d%02d%02d\n", YEAR, MONTH, DATE);
                                                     }
                                              }'

I can't comment on "admin ksh for windows", but above script doesn't have anything to do with ksh . It is pure awk for subtracting one day from the input supplied. It takes into account crossing New Year, and, partly, leap years plus some error checking. It does not add any number of days nor subtract more than one day.
Still it can serve as a starting point for your work on a ksh script to achieve what you want.

And, please be clear what you want - ksh (as in the title) or awk (as in post #1)?

Dear RudiC,

kindly , I want script to calculate date that is all more detail when you have 20150129 + 10 days the result will be 20150208. if you can provide me a script for that thank you very much.

Hello shaho87,

This is a forum, a place where we all are here to learn from each other. I have posted my script previously in POST#6 and requested you to try something with it, could you please try something with that script and let us know the issues/queries you face so that we can help you in that. By this way we can learn more and help more to each other which is the main aim of this forum. We all are open to do mistakes(consider my stetement in dev/testing/personal environments only) and learn from them, there is a famous quote for same.

Hope this helps.

Thanks,
R. Singh

Hello all

could you please some one explain below for me? thanks

_std_input_str=`dd count=$CONTENT_LENGTH bs=1 2> /dev/null`"&"
eval $(echo $_std_input_str |awk -F"&" '{for (i=1;i<NF;i++) print $i}')

Please open a new thread for a new request!

Above snippet looks strange with no context set.
It assigns $CONTENT_LENGTH bytes read from stdin plus a "&" char to the variable, wherever stdin is redirected to.
Then, assuming a field separator "&", it splits that text's fields into single line words, and executes those words using eval (pls be aware that using eval is deprecated as is may turn out to be dangerous).

So, curtly, it executes every word read from stdin, regardsless of its possible damage to your system.

The eval command has not been deprecated in the standards. It is still a mandatory special built-in utility in the shell in shells conforming to the POSIX standards and the Single UNIX Specifications.

However, I agree that it needs to be used judiciously. In general, eval should not be used to evaluate (or execute) user supplied strings.

The ksh can tell you version if you have command line editing (set -o vi or emacs) by typing escape + ctrl-V. Some systems have an enhanced ksh93 as dtksh (in the CDE package) or the like, even if they do not have ksh updated to ksh93.

---------- Post updated at 11:51 AM ---------- Previous update was at 10:37 AM ----------

Yes, recall the joys of SQL Injection attacks on web services. Know your inputs before you turn them into executables, like with eval or '... | bash'. Think worst case, like embedded '&', ';', '$', '"', "'", carriage returns, page feeds. line feeds, . . . .

Thanks all again ...

dears as i told in my first post I am new member that is why i dont know the rules very well for next post i will make new post for new request thanks again.