LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Declare variable in a script against a command (https://www.linuxquestions.org/questions/linux-newbie-8/declare-variable-in-a-script-against-a-command-604359/)

shipon_97 12-04-2007 04:30 AM

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 .....

druuna 12-04-2007 04:33 AM

Hi,

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

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

Hope this helps.

colucix 12-04-2007 04:40 AM

Another syntax for command substitution in /bin/bash is
Code:

X=$(ls -1 | wc -l)

shipon_97 12-04-2007 09:25 AM

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 ?

druuna 12-04-2007 10:19 AM

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.

shipon_97 12-05-2007 09:26 AM

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 ?

druuna 12-05-2007 09:35 AM

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.

chrism01 12-05-2007 05:48 PM

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

shipon_97 12-06-2007 03:08 AM

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) ?

colucix 12-06-2007 03:45 AM

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! :)

druuna 12-06-2007 11:11 AM

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).

colucix 12-06-2007 12:02 PM

@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!

druuna 12-06-2007 12:21 PM

@colucix: No harm done!

But if we are correct, what did cause the error? :)

colucix 12-07-2007 03:17 AM

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)?


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