2

I made a script to check if sdb or sdc drives are present. but even when I have both or one of them present the script is terminating. what is wrong with my script?

HDD1="sdb"
HDD2="sdc"

echo "Checking if necessary hard drives for NFS are present:"

function checkdrive 
{
DRIVE_CHK1=$(lsblk -o NAME | grep -w $1)
DRIVE_CHK2=$(lsblk -o NAME | grep -w $2)

if [ "$DRIVE_CHK1" == "$1" ]; then
    echo Found $1 drive.
    drive=$1
elif [ "$DRIVE_CHK2" == "$2" ]; then
    echo Found $2 drive.
    drive=$2
else
    echo ERROR! Could not locate hard drives.
    echo Terminating script.
    exit
fi
}

checkdrive $HDD1 $HDD2
6
  • 2
    Did you actually look at the output of lsblk -o NAME | grep -w sda1? Apr 8, 2015 at 12:55
  • 3
    You realize grep, by default, shows you lines that match, right? Apr 8, 2015 at 12:58
  • lsblk -o NAME shows me the block devices, each on one line by themselves (with the exception of partitions, like sda1 and so on).
    – zhongfu
    Apr 8, 2015 at 13:08
  • 1
    @zhongfu the default format in recent versions appears to be a tree- like output; the simple list requires an explicit -l switch Apr 8, 2015 at 13:11
  • simple test will do `if [[ -z "$(blkid | grep sdc)" ]] then; echo "no sdc"; fi'
    – Panther
    Apr 8, 2015 at 13:14

2 Answers 2

3

You run the script without the shebang. If you do not have the shebang, you need to run it with the preceding:

/bin/bash <script>

No matter if the script is executable or not...
When I run the script on my system with the command above, it works fine.

3
  • 1
    This does not address the actual problem in the script. Apr 8, 2015 at 12:59
  • @glennjackman I tested it with the command above, the output is: Checking if necessary hard drives for NFS are present: Found sdb drive If I disconnect it, it gives the appropriate message: ERROR! Could not locate hard drives Apr 8, 2015 at 13:00
  • @salim perfect! glad it works. Apr 8, 2015 at 13:36
1

Try "debugging" your script with set -x, just put it in your script as first command (you can also set this in your console, look).

[simmel]@[mars]$ set -x
[simmel]@[mars]$ echo Hello
+ echo Hello
Hello

Where the + sign shows the command which was executed and the line after that is the output of this command.

Check your output step by step and see if you get back, what your looking for.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .