cat setting variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cat setting variables
# 1  
Old 09-10-2002
cat setting variables

hi All
I have a file that has 4 lines:
1. yesterday's date (mm/dd/yyyy)
2. yesterday's day- dd
3. yesterday's month- mm
4. yesterday's year- yyyy

I want to read this file and place them in variables. how can I do this.

Please help.

thanks in advance!!

KS
# 2  
Old 09-10-2002
you can try a for loop.
# 3  
Old 09-10-2002
Question HOW?

I want the lines to be placed in seperate variables... could you kind enough to provide me with the commands?

KS
# 4  
Old 09-10-2002
man not much thought goes into your reply so not much goes into mine. heh

well in perl you can do it this way.

Code:
#! /usr/bin/perl -w

$FILE="test.file";

open FILE, "$FILE" or die "NO GO ($!)";

foreach (<FILE>) { chomp; push @contents, $_; };
foreach (@contents) { print $_,"\n" };

each element of the array contents has a line of the file

so:
$contents[0] has "mm/dd/yyyy"
$contents[1] has "dd"
$contents[2] has "mm"
$contents[3] has "yyyy"

to do it in shell scripting

Code:
#! /bin/ksh

i=1
file=test.file

for line in `cat test.file`;do
        if [ $i == 1 ]; then
                var1=$line
        elif [ $i == 2 ]; then
                var2=$line
        elif [ $i == 3 ]; then
                var3=$line
        elif [ $i == 4 ]; then
                var4=$line
        fi
i=$(($i+1))
done
echo VAR1=$var1
echo VAR2=$var2
echo VAR3=$var3
echo VAR4=$var4

both scripts are under the assumption that the file contains the following data

Code:
mm/dd/yyyy
dd
mm
yyyy


Last edited by Optimus_P; 09-10-2002 at 11:32 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Setting variables in UNIX

Hi, I am a beginner in Unix. Now I am learning setting up variables. However, I am receiving an error. Can anyone please help me with it My command as Test=unixprogramming returns the error command not found. (I am using FreeBSD Unix and in my terminal, it is ~% instead of $ . is the... (2 Replies)
Discussion started by: kgopan
2 Replies

2. UNIX for Dummies Questions & Answers

Cat and variables

I've been having trouble with cat and variables. If I do the following: var1=filex cat $var1 I get the contents of the file named filex If I do var2="X-0101\ 2-10-2013.txt" cat "$var2" I get cat : cannot open X-0101\ 2-10-2013.txt. I have tried to do cat $var2 without quotes as... (8 Replies)
Discussion started by: newbie2010
8 Replies

3. Shell Programming and Scripting

setting and displaying variables

Hello, I need a little help. 1. Edit /etc/profile so that all users are greeted upon login. 2. For the root account, set the prompt to something like "Danger!! root is doing stuff in \w", preferably in a bright color such as red or pink or in reverse video mode. Thanks for help. (4 Replies)
Discussion started by: zhshqzyc
4 Replies

4. UNIX for Dummies Questions & Answers

Setting Environment Variables

#!/bin/bash if ; then ASS1_DATA_DIR=./ echo $ASS1_DATA_DIR export ASS1_DATA_DIR echo "data dir" fi if ; then ASS1_OUTPUT_DIR=./ export ASS1_OUTPUT_DIR fi I want to create a new environment variable ASS1_DATA_DIR and ASS1_OUTPUT_DIR in bash and set them to the current... (4 Replies)
Discussion started by: bigubosu
4 Replies

5. Shell Programming and Scripting

Display variables in CAT area

Hi All, I've got a script to output YAML data, and I want to display data that's held inside variables inside one large CAT area. What's the easiest way to do this? cat << "END" --- classes: - general_image - $intro #Variable 1 - $mid #Variable 2 ... (2 Replies)
Discussion started by: glarizza
2 Replies

6. UNIX for Dummies Questions & Answers

Setting up variables

Hi all, I have a shell script that sets up the environment for an application running on UNIX - ksh. This script is run using: . ./script_name XX where XX is a parameter. I want to run it from another shell script but when I do it I don't get the envornment variables set up and the prompt... (3 Replies)
Discussion started by: solar_ext
3 Replies

7. UNIX for Advanced & Expert Users

setting some variables

i have a file .NAMEexport MY_NAME=JOE when i do this at the command prompt #. .NAME $echo MY_NAME $JOEi created a script called Run.sh . .NAME At the command prompt i did #sh Run.sh #echo $MY_NAMEit returns nothing. What have i missed out? (7 Replies)
Discussion started by: new2ss
7 Replies

8. Shell Programming and Scripting

Setting variables in a function

I'm not quite sure what I'm doing wrong here. I've go several jobs which print reports. Occassionally a printer will break down and reports need to be move to another printer. Rather than hard code the printer names in our scripts I'm trying to set these programatically using our function... (1 Reply)
Discussion started by: BCarlson
1 Replies

9. Shell Programming and Scripting

Setting up Environment Variables

Hi all, I am trying to set up some variables in a shell script. The variables contain values of various paths needed to run a java module. The problem is the variables dont seem to be setting at all. here is what i am trying to do : JAR_HOME=/home/was5/bdcms/scheduledjobs/lib export... (6 Replies)
Discussion started by: rpandey
6 Replies

10. UNIX for Dummies Questions & Answers

setting environment variables ???

Hello, I want to set some environment variables with this script: ip=$@ echo Remote Computer: $ip PERLDB_OPTS="CallKomodo=$ip:9000 RemotePort=$ip:9010 PrintRet=0" export PERLDB_OPTS PERL5LIB=/opt/komodo export PERL5LIB echo PERLDB_OPTS: $PERLDB_OPTS echo PERL5LIB: $PERL5LIB But it... (5 Replies)
Discussion started by: Gargamel
5 Replies
Login or Register to Ask a Question