LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script: problem with [[ ]] (test) (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-problem-with-%5B%5B-%5D%5D-test-4175437163/)

impert 11-14-2012 03:17 PM

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.

impert 11-14-2012 03:52 PM

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.

Reuti 11-14-2012 04:46 PM

Did you notice the space behind (/dev/sda12 )? If you use quotation marks with test it might not work too.

impert 11-15-2012 06:10 AM

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.

catkin 11-15-2012 08:49 AM

Quote:

Originally Posted by impert (Post 4829541)
Using test instead of [[ ]] fixes it. (I thought they were identical).

What is the difference between test, [ and [[ ?

rknichols 11-15-2012 10:27 AM

Quote:

Originally Posted by impert (Post 4829523)
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.

impert 11-15-2012 04:30 PM

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

impert 11-15-2012 04:35 PM

Quote:

What if this weren't a rhetorical question?
Then I should attempt to answer it.


All times are GMT -5. The time now is 08:53 PM.