Find the answer to your Linux question:
Results 1 to 2 of 2
I am writing a bash script that executes dmidecode to get hardware information from the BIOS. It writes this information to a file on the system. I am then using ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2004
    Posts
    7

    Bash Question


    I am writing a bash script that executes dmidecode to get hardware information from the BIOS. It writes this information to a file on the system. I am then using grep to pull out the information that I need to reference and store this in a variable. The problem I am having is that what is being stored in the variable doesn't match my if statement. When I echo back what is being stored it seems that there are leading and trailing spaces or tabs. Is there anyway to get rid of these. This is what my script looks like currently:

    Code:
    #!/bin/bash
    
    hwmodel=$(grep -m 1 Product /tmp/data.txt | sed 's/[ ][^ ]*$//')
    
    if [ "$hwmodel" = "Product Name: HP Compaq nc6000 (DS800P#ABG)' ]
    then
      echo "HP Compaq nc6000"
      exit 0
    fi
    
    echo "-$hwmodel-"
    exit 0
    Even running the grep through sed doesn't seem to remove the spaces. Is there anyone that can help?

  2. #2
    Just Joined!
    Join Date
    Nov 2004
    Posts
    7
    Ok, after much searching I have the answer:

    Code:
    hwmodel=$(grep -m 1 Product /tmp/data.txt | sed "s/^[ \t]*//;s/[ \t]*$//")

Posting Permissions

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