setting a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers setting a variable
# 1  
Old 03-06-2007
setting a variable

In my script, I have the following command....

du -sk `ls -ltd sales12|awk '{print $11}'`|awk '{print $1}'

it returns the value

383283

I want to modify my script to capture that value into a variable. So, I try doing the following...

var1=`du -sk `ls -ltd sales12|awk '{print $11}'`|awk '{print $1}'`

but that don't work. I know I can do this...

du -sk `ls -ltd sales12|awk '{print $11}'`|awk '{print $1}' > file.dat
var1=`cat file.dat`

and this will work, but then I have a file I need to clean up afterwards.

Any suggestions on how I can within one line get my variable to be set?

Thank you in advance.
# 2  
Old 03-06-2007
however here is your command:
Code:
myvariable=$(du -sk `ls -ltd sales12|awk '{print $11}'`|awk '{print $1}')

# 3  
Old 03-06-2007
What's the use of "ls -ltd sales12"???

Code:
#!/usr/bin/ksh

typeset -i var1
var1=`du -sk sales12 | awk '{ print $1 }'`

# 4  
Old 03-07-2007
ls -ltd will give long list display of directory entries sorted by modification time..

Ex:

u142115@linux2alm:~/aps/aps4/product/den> ls -ltd day*
drwxr-xr-x 2 u142115 users 48 2006-12-05 10:14 day8
drwxr-xr-x 2 u142115 users 312 2006-12-05 10:03 day7
drwxr-xr-x 2 u142115 users 224 2006-11-29 12:08 day4
drwxr-xr-x 2 u142115 users 200 2006-11-28 19:27 day6
drwxr-xr-x 2 u142115 users 400 2006-11-28 11:42 day3
drwxr-xr-x 2 u142115 users 160 2006-11-28 10:00 day5
# 5  
Old 03-07-2007
Quote:
Originally Posted by jacoden
ls -ltd will give long list display of directory entries sorted by modification time..

Ex:

u142115@linux2alm:~/aps/aps4/product/den> ls -ltd day*
drwxr-xr-x 2 u142115 users 48 2006-12-05 10:14 day8
drwxr-xr-x 2 u142115 users 312 2006-12-05 10:03 day7
drwxr-xr-x 2 u142115 users 224 2006-11-29 12:08 day4
drwxr-xr-x 2 u142115 users 200 2006-11-28 19:27 day6
drwxr-xr-x 2 u142115 users 400 2006-11-28 11:42 day3
drwxr-xr-x 2 u142115 users 160 2006-11-28 10:00 day5

"ls -ltd sales12" will display ONLY a long listing of "sales12".

"ls -ltd day*" will display a list of files/directories because of the wildcard.

Why you need a long listing if you are only interested in the name ("awk '{print $11}'"? There aren't even 11 fields in that output.

"ls -td day*" will display exactly the same without the need to parse the output with "awk".

Last, but not least, if your "ls" command produces a list, then "var1" will also contain a list of sizes, and not a single value.
# 6  
Old 03-07-2007
Quote:
Originally Posted by sb008
"ls -ltd sales12" will display ONLY a long listing of "sales12".

"ls -ltd day*" will display a list of files/directories because of the wildcard.

Why you need a long listing if you are only interested in the name ("awk '{print $11}'"? There aren't even 11 fields in that output.

"ls -td day*" will display exactly the same without the need to parse the output with "awk".

Last, but not least, if your "ls" command produces a list, then "var1" will also contain a list of sizes, and not a single value.

Thank you for ur valuable advice...Am a newbie "really working hard to learn Unix in depth"...I think all these will surely help me filling my gaps...and understanding that learning Unix is not soemthing we can just get from books..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Setting a variable in unix

Hi , Whenever i want to start tomcat server I need to go to <tomcatdir>\bin and execute startup.sh file. How would i make this happen whenever i start my machine? Also want to know How would i set the variable at startup. for example I want to set JAVA_HOME and JRE_HOME whenever the... (3 Replies)
Discussion started by: pinga123
3 Replies

2. Shell Programming and Scripting

Setting a variable within if block

Hi, i have a variable which i would like to set inside an if block for example IS_VAR=0 if then IS_VAR=1 fi echo IS_VAR the last echo statement gives 0.So setting variables in the if block doesnt have effect outside the block?Is there any workaround for this? Thanks , Padmini (11 Replies)
Discussion started by: padmisri
11 Replies

3. Programming

Setting Environment variable..!

Hi, I already have one CPP program which invokes the C program.And the C program contains whole function definitions..!This is a working program..I have to enable the logs in both CPP as well as in the C program ..!So I am reading the enviornmental variable log path from the CPP and doing the... (2 Replies)
Discussion started by: Kattoor
2 Replies

4. Shell Programming and Scripting

Help with setting a variable!

I am working within a while loop and i am trying to set a variable that will read out each count of the files. the problem is the count variable i have set up gives me a total and not the individual count of each file. in the data area there is 4 abc.dat and 1 def.dat. how can i do this??? ... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

5. Shell Programming and Scripting

Setting variable

How do you set a varible with information that contains a string and also another variable? For example: subject="Attention: $name / This $type needs your attention" The $xxxx are of course other variables that I instantiated earlier. Is it like Java where you have to use double quotes and... (1 Reply)
Discussion started by: briskbaby
1 Replies

6. UNIX for Dummies Questions & Answers

Setting a variable (need syntax help)

I need some syntax help (working in a bash shell) I have a variable which is a filename with an extension, and I need to create another variable with the same name but a different extension To explain, the input file should be called something like "filename.L1" and the output file should be... (1 Reply)
Discussion started by: Slanter
1 Replies

7. Shell Programming and Scripting

Variable setting help please

L=0 cat test.sh | while read line do L='expr $1 + 1' echo $L done echo $l >>> the echo $L at the end produces 0 but i actually want it to produce the number of lines - any idea why this is happening? (16 Replies)
Discussion started by: penfold
16 Replies

8. UNIX for Dummies Questions & Answers

Setting a variable

I want to set a variable to be any number of dashes. Rather than doing the following: MYVAR="------------------" I'd like to be able to set the variable to, say, 80 dashes but don't want to have to count 80 dashes. Is there a way to do this? (2 Replies)
Discussion started by: photh
2 Replies

9. Programming

setting CC variable

I am trying to install GCC-3.1.1 on an SGI Indigo2. I already have MIPSpro 7.2.1 installed. However, when I try to configure GCC-3.1.1, I get the message "cc ERROR: cc -o conftest -g failed, You must set the environment variable CC to a working compiler." What is the name of the MIPSpro c++... (1 Reply)
Discussion started by: mdbanas
1 Replies

10. UNIX for Dummies Questions & Answers

setting CC variable

I am trying to install GCC-3.1.1 on an SGI Indigo2. I already have MIPSpro 7.2.1 installed. However, when I try to configure GCC-3.1.1, I get the message "cc ERROR: cc -o conftest -g failed, You must set the environment variable CC to a working compiler." What is the name of the MIPSpro c++... (1 Reply)
Discussion started by: mdbanas
1 Replies
Login or Register to Ask a Question