LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-04-2007, 04:30 AM   #1
shipon_97
Member
 
Registered: Oct 2005
Location: Bangladesh
Posts: 504

Rep: Reputation: 31
Declare variable in a script against a command


Friends ,

I m going to make a script and got a problem . In script , I want to put a command output into a varialbe , Suppose,

"ls -1|wc -l" , is a command which generate a numarical value . Now I want to put it into a variable, so that I can compare it with other varialbe , Like

X = ls -1|wc -l

Is it possible to do ? Plz give me some suggestions .....
 
Old 12-04-2007, 04:33 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This is one way: X="`command`"

The backticks are the important part. The double quotes are not (always) needed.

Hope this helps.
 
Old 12-04-2007, 04:40 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Another syntax for command substitution in /bin/bash is
Code:
X=$(ls -1 | wc -l)
 
Old 12-04-2007, 09:25 AM   #4
shipon_97
Member
 
Registered: Oct 2005
Location: Bangladesh
Posts: 504

Original Poster
Rep: Reputation: 31
Script with variable decleration

Dear Friends ,

Thx for ur help . Now I have two questions regarding this matter

1) Suppose I have a folder "count1" which has 10 contents and another folder "count2" which has 9 contents .I want to compare this contents and make a script like the following :

=============================================
[root@server ~]# cat test1.sh
cd /root/count1
ls -1|wc -l
X=$(ls -1|wc -l)

cd /root/count2
ls -1|wc -l
Y=$(ls -1|wc -l)


if($X=$Y); then
echo "Contents are same"
else
echo "Contents are not same"
fi
=============================================

But in output it shows the follwoing error :
==================================================
[root@server ~]# sh test1.sh
60
60
test1.sh: line 10: 60=60: command not found
Contents are not same
==================================================

Plz help regarding this matter ....


2) My another question is :

suppose in "count1" folder i have the follwoing files :

========================================================
drwxrwxr-x 2 100 users 4096 Jan 10 2006 sequence
drwxrwxr-x 3 100 users 4096 Jan 10 2006 tcl
drwxrwxr-x 33 100 users 16384 Nov 13 2005 test
drwxrwxr-x 2 100 users 4096 Jan 10 2006 txn
drwxrwxr-x 2 100 users 4096 Jan 10 2006 xa
====================================================

Now here in above script i want to put the last date "Jan 10 2006" into a variable so that I can compare with other one file , i.e., I want to compare file as last date-wise , is it possible ?
 
Old 12-04-2007, 10:19 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Question number 1)

You used this if($X=$Y) to compare the two, which is incorrect. When using numbers you should do something like this: if [ "$X" -eq "$Y" ]

The -eq part tells bash that it is comparing numbers, not strings. If you need to compare strings something like this: if [ "$x" == "$y" ] needs to be used.

BTW all is explained in this Bash manual (see chapter Chapter 7. Tests for the above case).

Question number 2)

There is more then one way to do this, I personally like awk: awk '{ print $6, $7, $8 }' would 'cut out' the part you want to see.
Use something like this: Z="`ls -l | awk '{ print $6, $7, $8 }'`" to fill a variable (Z in this case) with the output.

Hope this clears things up a bit.
 
Old 12-05-2007, 09:26 AM   #6
shipon_97
Member
 
Registered: Oct 2005
Location: Bangladesh
Posts: 504

Original Poster
Rep: Reputation: 31
Thx druuna ,

According ur advice , i got the following result from a folder :


[root@server ~]# ls -l |awk '{ print $6, $7, $8 }'

Oct 14 00:32
Dec 5 07:15
Dec 4 06:58
Dec 5 07:21
Oct 14 11:32
Oct 14 00:31
Oct 14 00:31
Oct 26 22:36
Dec 4 07:10
Dec 5 07:09


But , My question is , I want to find the last date of the above date names . Using which command I get to find the last date(Here , Dec 5 )of the folder ? And How can I put this "Dec 5" into a variable ? Can u plz help me ?
 
Old 12-05-2007, 09:35 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

If you want the last entry you can use tail -1.

Z="`ls -l | awk '{ print $6, $7, $8 }'`"

will become:

Z="`ls -l | tail -1 | awk '{ print $6, $7, $8 }'`"

Hope this helps.

BTW: If you want to sort the output old -> new, use ls -ltr instead, this will make sure that the newest file is always the last entry.
 
Old 12-05-2007, 05:48 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
Yep,
ls -lt newest at top
ls -ltr oldest at top
both sort by date, so you just use whichever, pipe through head -1 and then awk to get the date fields
 
Old 12-06-2007, 03:08 AM   #9
shipon_97
Member
 
Registered: Oct 2005
Location: Bangladesh
Posts: 504

Original Poster
Rep: Reputation: 31
compare string how to ?

Thx druuna again ,

using the following command , i can store my last date information in variable Z .
Z="`ls -lrt | awk '{ print $6, $7, $8 }'`"
Y="`ls -l | tail -1 | awk '{ print $6, $7, $8 }'`"

But in this moment i cannot compare both , i.e., when i m going to compare like following way then it shows error .

if [ "$x" == "$y" ]

is it the perfect way to compare the above output (05 Dec 2007) ?
 
Old 12-06-2007, 03:45 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Just a couple of notes:

1. if your aim is to compare files based on timestamp you can always use the test "newer than" or "older than". See man test for details.

2. if you really want to extract the timestamp from the ls command you can consider the option --time-style to customize the date format and to choose one suitable for your needs. For example, in simple comparisons between dates the most convenient format is YYYYMMDD which permits numerical comparisons (not only string comparison). For example, suppose you have
Code:
$ ls -l
-rw-r--r-- 1 colucix users 11 Jul 11 23:45 testfile_1
-rw-r--r-- 1 colucix users 11 Dec  6 10:36 testfile_2

$ timestamp_1=$(ls -l --time-style=+%Y%m%d testfile_1 | awk '{print $6}')
$ echo $timestamp_1
20070711

$ timestamp_2=$(ls -l --time-style=+%Y%m%d testfile_2 | awk '{print $6}')
$ echo $timestamp_2
20071206

$ if [ $timestamp_1 -eq $timestamp_2 ]
> then
>    echo "timestamps are the same"
> else
>    echo "timestamps differ"
> fi
timestamps differ
3. if you really want to use string comparison as in your last post, the correct syntax is
Code:
if [ "$x" = "$y" ]
then
   <commands here>
fi
with a single equal sign! Cheers!
 
Old 12-06-2007, 11:11 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@colucix: A single or a double equal sign should behave the same.

This quote from the Advanced Bash-Scripting Guide, chapter 7.3:
Quote:
=

is equal to

if [ "$a" = "$b" ]

==

is equal to

if [ "$a" == "$b" ]

This is a synonym for =.

The == comparison operator behaves differently within a double-brackets test than within single brackets.
[[ $a == z* ]] # True if $a starts with an "z" (pattern matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).

[ $a == z* ] # File globbing and word splitting take place.
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching).

# Thanks, Stéphane Chazelas
I always questions everything, so I did actually try it: It works the way it is described, both 1 or 2 equal signs give the same result. And even if the OP used double brackets, it works (double quoted => literal matching).

The Bash document is rather new and probably based on bash 3.X (could not find that info). It could be that all the above is not true when using a bash version earlier then 3.x (bash --version to check).
 
Old 12-06-2007, 12:02 PM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
@druuna: You're right! It was so strong my habit to use a single equal in bash, that I didn't think about this possibility. Thank you for notice!
 
Old 12-06-2007, 12:21 PM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@colucix: No harm done!

But if we are correct, what did cause the error?
 
Old 12-07-2007, 03:17 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The last post from the OP is somehow confusing. From the assigment
Code:
Z="`ls -lrt | awk '{ print $6, $7, $8 }'`"
Z will contain more than one date, since it not contains any tail command.

@shipon_97: can you post the last version of your script (or command lines)?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script: put the output of a command into a variable. poincare999 Programming 4 11-04-2007 11:53 AM
Windows batch script: SET command as variable Dr_Death_UAE Programming 10 09-09-2007 07:18 PM
Where should I declare the global variable ahm_irf Linux - Kernel 5 05-11-2007 11:36 AM
Shell script: How to include a variable between apostrophes within a command guarriman Programming 3 02-23-2007 03:12 AM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 09:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration