Find the answer to your Linux question:
Results 1 to 2 of 2

Thread: Bash Question

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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:

    hwmodel=$(grep -m 1 Product /tmp/data.txt | sed 's/[ ][^ ]*$//')
    if [ "$hwmodel" = "Product Name: HP Compaq nc6000 (DS800P#ABG)' ]
      echo "HP Compaq nc6000"
      exit 0
    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
    Ok, after much searching I have the answer:

    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