Find the answer to your Linux question:
Results 1 to 4 of 4
In an effort to get my RAID array to mount at boot time, I am writing a shell script to do all the work for me. What I would like ...
  1. #1
    Just Joined!
    Join Date
    Nov 2004
    Posts
    48

    Checking the status of an array

    In an effort to get my RAID array to mount at boot time, I am writing a shell script to do all the work for me.

    What I would like to do is use a boolean expression inside an if/else statement to see if the array /dev/md0 is currently active.

    To check the status of an array by hand, I issue the command "sudo cat /proc/mdstat", which returns:


    Personalities : [raid1]
    md0 : active raid1 sda1[0] sdb1[1]
    488383936 blocks [2/2] [UU]

    unused devices: <none>


    The if test i have is as follows:

    if [cat /proc/mdstat|grep "md0 : active"]

    When I run the script it gives me a "no such file or directory error".

    Does anyone have any idea how to get this work? I am new to shell scripting, so I am sure I am missing something simple. Here is my entire if statement structure:


    if [cat /proc/mdstat|grep "md0 : active"]
    then
    echo "RAID array exsists!" &
    #Check if array is mounted
    else
    echo "Re-adding array: /dev/md0" &
    mdadm -A /dev/md0 /dev/sda1 /dev/sdb1
    echo "Mounting /dev/md0 to /var/shared" &
    mount /dev/md0 /var/shared
    echo "Changing ownership of /var/shared" &
    echo "Changing group of /var/shared" &
    #chown & chgrp /var/shared
    echo "Restarting samba with: /etc/init.d/samba" &
    /etc/init.d/samba restart
    fi

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Looks the common mistake of using
    Code:
    [cat
    instead of
    Code:
    [ cat
    Note the space between "[" and "cat" -- the character "[" is actually a command and so needs whitespace.

    Using the [CODE] blocks helps forum folks see troubles like this. If that is not the problem, please post again ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Just Joined!
    Join Date
    Nov 2004
    Posts
    48
    Thank you! It was a combination of that, and the fact that the use of single and double quotes still had me confused! Here is the code that works.

    Code:
    if [ "cat /proc/mdstat|grep md0 : active" ]
                    then
                            echo "RAID array exsists!" &
                            #Check if array is mounted
            else
                    echo "Re-adding array: /dev/md0" &
                    #mdadm -A /dev/md0 /dev/sda1 /dev/sdb1
                    echo "Mounting /dev/md0 to /var/shared" &
                    #mount /dev/md0 /var/shared
                    echo "Changing ownership of /var/shared" &
                    echo "Changing group of /var/shared" &
                    #chown & chgrp /var/shared
                    echo "Restarting samba with: /etc/init.d/samba" &
                    #/etc/init.d/samba restart
            fi

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    OK, you got past the syntax error, but there is still a problem with the if (a semantic error).

    The if takes a command and looks at the exit status, but you have if command command.

    My suggestion is to do a few small test cases. You may want to refer to the very good tutorial around this topic: http://www.tldp.org/LDP/abs/html/testconstructs.html ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...