Operating System - HP-UX
1834144 Members
2309 Online
110064 Solutions
New Discussion

Re: variable integer not passed

 

variable integer not passed

Why doesn't the following script work?
#!/bin/ksh
integer Line=0
Lines=$(cat /root/.sh_history | wc | awk '{print $1}' )
print "total lines to read in History file is $Lines"
history -${Lines} > ${Work_file}

Output is....
total lines to read in History file is 268

./Audit_root_Daily[90]: 1^Ill: syntax error
10 REPLIES 10
Rodney Hills
Honored Contributor

Re: variable integer not passed

Is variable Work_file defined?

The command looks ok otherwise.

Rod Hills
There be dragons...

Re: variable integer not passed

yes, Work_file is defined..

Work_file=/usr/local/work/${Prgrm_name}.wrk

I clipped this from a 300 line routine that is failing me. Originally it worked interactively but would never work batch.
The error has something unique to the variable Lines not passing a value to the history command.
Rodney Hills
Honored Contributor

Re: variable integer not passed

"history" is an alias to "fc -l". Is it possible that aliases are not being setup when you run in batch?

Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: variable integer not passed

Is it possible you have some control character in your script file on the history command? Try displaying with cat -v scriptfile to see.

Rod Hills
There be dragons...

Re: variable integer not passed

I'm sure it is not the alias that is the problem. With the current script, I get the following error interactively or batch...

1^Ill: syntax error

but if I hard code a number into the script that I know will work, all is fine. I would like to use a huge number to cover the entire file, but history doesn't like that at all when run batch. That's why I count the lines in the history file first, then use that variable to list out the file. For instance, if I hard code 268 into the script, all will be fine.

Re: variable integer not passed

No, cat -v displays no control characters.

Have you tried this yourself? and does it work for you? I have a feeling passing an option as a variable is not possible through scripting.
James R. Ferguson
Acclaimed Contributor

Re: variable integer not passed

Hi:

The shell history file contains more than simple ASCII characters.

As an alternative to 'cat|wc|awk' try:

# lines=$(awk 'END {print i};{i=1;while (getline) i++}' /root/.sh_history)

Regards!

...JRF...
John Kittel
Trusted Contributor

Re: variable integer not passed

I tried your script snippet as given, with the addition that I defined Work_file prior to using it, - and I get similar, not identical error.

But, if I change it to a posix shell script, it works. Change first line to #!/bin/sh

Not sure yet why sh likes it but ksh doesn't, ... still tinkering.

- John
John Kittel
Trusted Contributor

Re: variable integer not passed

Ok, it seems to work as a ksh script if my default shell is ksh, and it works as a posix script if my default shell is sh. I suspect your root default shell is also posix sh, so you should use posix script.

- John
Dave La Mar
Honored Contributor

Re: variable integer not passed

Matt-
I believe you should be looking around line ninety in the script.
./Audit_root_Daily[90]: 1^Ill: syntax error
Also, as you have found, large numbers are not acceptable.
I tested up to 127 as a value for Lines without issue.

Regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."