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 ..... |
Hi,
This is one way: X="`command`" The backticks are the important part. The double quotes are not (always) needed. Hope this helps. |
Another syntax for command substitution in /bin/bash is
Code:
X=$(ls -1 | wc -l) |
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 ? |
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. |
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 ? |
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. |
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 |
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) ? |
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 Code:
if [ "$x" = "$y" ] |
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:
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). |
@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!
|
@colucix: No harm done!
But if we are correct, what did cause the error? :) |
The last post from the OP is somehow confusing. From the assigment
Code:
Z="`ls -lrt | awk '{ print $6, $7, $8 }'`" @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. |