LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-14-2012, 03:17 PM   #1
impert
Member
 
Registered: Feb 2009
Posts: 282

Rep: Reputation: 54
Bash script: problem with [[ ]] (test)


Hi all,
I'm trying to write a script to do fsck on shutdown, rather than having to wait for big partitions when booting. (Why doesn't everyone do this? Am I the only impatient person using Linux? Or is it a Bad Idea?)

I've run into a problem that puzzles me. To avoid telling e2fsck to check the root partition while it is mounted, I pull its device name from /etc/mtab. The problem is that [[ ]] considers this name different from the same name entered in another way. This happens in my script; it also happens if I enter the commands manually in a terminal:

Code:
impert@pc:~$ rootpart=$(grep ' / ' /etc/mtab |grep '/dev' | sed 's:/ .*$::')
impert@pc:~$ echo $rootpart
/dev/sda12
impert@pc:~$ part=/dev/sda12
impert@pc:~$ echo $part
/dev/sda12
impert@pc:~$ if [[ ! $part == $rootpart ]]  ;then
> echo "part ($part) is not the same as rootpart ($rootpart)"
> else
> echo "part ($part) is the same as rootpart ($rootpart)"
> fi
part (/dev/sda12) is not the same as rootpart (/dev/sda12 )
It doesn't seem to matter which way I do the test:

Code:
impert@pc:~$ if [[ $part == $rootpart ]]  ;then echo "part ($part) is the same as rootpart ($rootpart)"; else echo "part ($part) is not the same as rootpart ($rootpart)"; fi
part (/dev/sda12) is not the same as rootpart (/dev/sda12 )
My apologies if this is not the right forum: I find questions on bash scripts in Newbies, Programming, Software, and General.
 
Old 11-14-2012, 03:52 PM   #2
impert
Member
 
Registered: Feb 2009
Posts: 282

Original Poster
Rep: Reputation: 54
OK; solved, or partially solved.
Using test instead of [[ ]] fixes it. (I thought they were identical). Why it occurs with [[ ]] remains a mystery to me.

Last edited by impert; 11-14-2012 at 03:55 PM.
 
Old 11-14-2012, 04:46 PM   #3
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,328

Rep: Reputation: 254Reputation: 254Reputation: 254
Did you notice the space behind (/dev/sda12 )? If you use quotation marks with test it might not work too.
 
1 members found this post helpful.
Old 11-15-2012, 06:10 AM   #4
impert
Member
 
Registered: Feb 2009
Posts: 282

Original Poster
Rep: Reputation: 54
Quote:
Did you notice the space behind (/dev/sda12 )? If you use quotation marks with test it might not work too.
Aha!
No, I didn't notice the space, and I should have looked more closely. Thank you Reuti.
 
Old 11-15-2012, 08:49 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by impert View Post
Using test instead of [[ ]] fixes it. (I thought they were identical).
What is the difference between test, [ and [[ ?
 
Old 11-15-2012, 10:27 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,399

Rep: Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486Reputation: 1486
Quote:
Originally Posted by impert View Post
Hi all,
I'm trying to write a script to do fsck on shutdown, rather than having to wait for big partitions when booting. (Why doesn't everyone do this? Am I the only impatient person using Linux? Or is it a Bad Idea?)
Should a system need to shut down right now because a UPS has indicated that its battery is nearly exhausted, running a lengthy fsck could be a Bad Thing(tm). A common enterprise solution is to disable all of the automatic fs checks ("tune2fs -c 0 -i 0") and do those checks during scheduled maintenance time.
 
Old 11-15-2012, 04:30 PM   #7
impert
Member
 
Registered: Feb 2009
Posts: 282

Original Poster
Rep: Reputation: 54
@catkin
Thanks for the link; for some reason it doesn't work for me at the moment. I'll try it again later.

@rknichols
Quote:
Should a system need to shut down right now because a UPS has indicated that its battery is nearly exhausted, running a lengthy fsck could be a Bad Thing(tm). A common enterprise solution is to disable all of the automatic fs checks ("tune2fs -c 0 -i 0") and do those checks during scheduled maintenance time.
My intention is to give myself an alternative to the ordinary shutdown button, and to do the fs checks while I'm cleaning my teeth and getting ready for bed. Not quite an enterprise solution, I agree.


One thing I haven't seen clearly explained is what should govern the choice of max mount count and check interval. (They seem often to be 26 mounts and 180 days, which smacks of the 1970s: start the System 360 on Monday morning, do a tape backup and shutdown on Friday evening, and check fs every 6 months.)

So what corrupts file systems? Is it mounting/unmounting, reading files, writing to them, fragmentation, mechanical stuff like misaligned heads and misbehaving drive motors, magnetic degradation through heat, vibration, or passage of time, cosmic radiation, or some combination of all of the above and other things I haven't thought of?

And in 2012 with fairly modern hardware and a journalled file system like ext3 or ext4 what are reasonable choices for max mount count and check interval?

Any thoughts?
 
Old 11-15-2012, 04:35 PM   #8
impert
Member
 
Registered: Feb 2009
Posts: 282

Original Poster
Rep: Reputation: 54
Quote:
What if this weren't a rhetorical question?
Then I should attempt to answer it.
 
  


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
Problem With Arguments Test in Bash Script dcparris Linux - Software 8 03-20-2012 07:07 AM
[SOLVED] Bash Script problem running If test on user entered variables Regnets1 Linux - Newbie 3 02-17-2012 02:21 PM
help with Bash script (test) Benanzo Linux - Software 1 04-09-2007 03:18 AM
Test output from bash script estratos Programming 6 11-16-2006 09:01 PM
bash script 'test' question linmix Programming 5 11-29-2005 11:49 AM


All times are GMT -5. The time now is 03:55 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration