Assign perl output to ksh shell variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign perl output to ksh shell variable
# 1  
Old 08-06-2010
Assign perl output to ksh shell variable

Hello,
I am writing a ksh script on an AIX system. I need to get the date and time from a file into a variable. I found the following perl script from another post on this site and modified it slightly to output the format I need:

Code:
perl -e '@d=localtime ((stat(shift))[9]); "%02d%02d%04d_%02d%02d%02d", $d[3],$d[4]+1,$d[5]+1900,$d[2],$d[1],$d[0]' <filename>


I need to get the output of the above perl into a variable in my ksh script. Is this possible to do or would I need to write the script in perl instead?

Thanks!

Last edited by Scott; 08-06-2010 at 04:42 PM.. Reason: Please use code tags
# 2  
Old 08-06-2010
Code:
var=`perl -e '@d=localtime ((stat(shift))[9]); "%02d%02d%04d_%02d%02d%02d", $d[3],$d[4]+1,$d[5]+1900,$d[2],$d[1],$d[0]' <filename>`

or
Code:
var=$(perl -e '@d=localtime ((stat(shift))[9]); "%02d%02d%04d_%02d%02d%02d", $d[3],$d[4]+1,$d[5]+1900,$d[2],$d[1],$d[0]' <filename>)

# 3  
Old 08-06-2010
Hi Franklin52,
Thanks for the quick response. I tried both things you suggested but neither of them worked. I've noticed that code that works on unix does not necessarily work on AIX.

If I could redirect the output from the perl statement to a file then I could open the file and read the data. Do you know of any way to do that?

Thanks
# 4  
Old 08-07-2010
You can redirect the output to a file like:
Code:
perl -e '...' filename > outfile

If the output is 1 line you can read it in a variable like:
Code:
< outfile read var
echo "$var"

otherwise you have to use a loop to read the lines:
Code:
while read var
do
  # do your stuff with $var
done < outfile

This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 08-09-2010
Franklin52,
Thanks! This works great!

Swimp
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh PS4 variable assign to `date` output

Hi guys, Is there a way to assign curent time to PS4 variable in ksh. My goal is to have each line produced by 'set -x' command to have a time stamp. Here is my code: $cat test #!/usr/bin/ksh export PS4="`date` " set -x echo "TRACE LINE ONE" echo "I WILL SLEEP FOR 10 SEC" sleep 10... (2 Replies)
Discussion started by: aoussenko
2 Replies

2. Shell Programming and Scripting

Perl help - how to assign output of perl to variable

Hi, guys, i have a script i inherited from a coworker but i'm not perl savy. The script works but i would like it to work better. I want to run this command ./ciscomgrtest.pl -r "show version" -h hosts.router and have the script goto each router in the hosts.router file and run the command... (2 Replies)
Discussion started by: whipuras
2 Replies

3. Shell Programming and Scripting

Assign output to dynamic variable

Hi Folks, I am trying to assign a value from the command to a dynamic variable. But I am not getting the desired output.. I am sure something is wrong so i need experts advise. There will be multiple files like /var/tmp/server_1, /var/tmp/server_2, /var/tmp/server_3, having different server... (6 Replies)
Discussion started by: ganga.dharan
6 Replies

4. Shell Programming and Scripting

Assign field value in a .csv file to a variable using ksh

Hi, I'm new to the scripting world... I want to know that how can I assign the the field value(that has multiple lines) of a .csv file to a variable??? Model of my .csv file : 1 Poppy 5 2 red 6 3 black 5 4 white 8 and so on,the list... (4 Replies)
Discussion started by: srim
4 Replies

5. Shell Programming and Scripting

How to assign record count output of isql to a shell variable ?

isql select count(*) from Table eof How to assign record count output of isql query to a shell variable ? (4 Replies)
Discussion started by: vikram3.r
4 Replies

6. Shell Programming and Scripting

assign awk output to bash variable

greetings all, I am have a heck of a time trying to accomplish a very simple thing. I have an array of "shortname<spaces>id" created from a dscl output. I want to assign shortname=word1 and id=word2. I have tried shortname=$(${textArray} | awk '{print $1}') - and get 'awk : cannot open... (3 Replies)
Discussion started by: macnetdaemon
3 Replies

7. Shell Programming and Scripting

how to assign the output of the interective script to the variable

Hi, I work in ksh88. I have an interective script which prompts the user for the input and returns numeric value depending on the input provided. I need to call this script inside another script and then assign the resulting output the the variable. The call like that A=`my script` obviously... (6 Replies)
Discussion started by: aoussenko
6 Replies

8. Shell Programming and Scripting

how to assign sql output data to shell script variable

Hi Guys ! I am new to unix and want to find out how we can make sql statement data to shell script variable? Any help/suggestion is greatly appreciated -Chandra (1 Reply)
Discussion started by: kattics
1 Replies

9. Shell Programming and Scripting

hot to assign output to a variable

I want to assign a comment to a veriable for example my program head -1 myfile I want to assıgn output to a variable (1 Reply)
Discussion started by: walnut
1 Replies

10. UNIX for Dummies Questions & Answers

how to assign an output to a variable

Hi, I am giving a grep command, and i am getting the output. i want to store it in a variable for eg a = grep '12345' /dir/1/2/log.txt ( the output is number) b= grep 'basic' /dir/1/2/log1.txt (in this case the output is character) so how to assign the output of grep to a variable ... (1 Reply)
Discussion started by: vasikaran
1 Replies
Login or Register to Ask a Question