Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 .....
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 ?
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.
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 ?
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) ?
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
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.