How do I compare strings in Bourne Shell?

泄露秘密 提交于 2019-12-10 01:16:15

问题


I need to compare strings in shell:

var1="mtu eth0"

if [ "$var1" == "mtu *" ]
then
    # do something
fi

But obviously the "*" doesn't work in Shell. Is there a way to do it?


回答1:


bash

Shortest fix:

if [[ "$var1" = "mtu "* ]]

Bash's [[ ]] doesn't get glob-expanded, unlike [ ] (which must, for historical reasons).


bash --posix

Oh, I posted too fast. Bourne shell, not Bash...

if [ "${var1:0:4}" == "mtu " ]

${var1:0:4} means the first four characters of $var1.


/bin/sh

Ah, sorry. Bash's POSIX emulation doesn't go far enough; a true original Bourne shell doesn't have ${var1:0:4}. You'll need something like mstrobl's solution.

if [ "$(echo "$var1" | cut -c0-4)" == "mtu " ]



回答2:


Use the Unix tools. The program cut will happily shorten a string.

if [ "$(echo $var1 | cut -c 4)" = "mtu " ];

... should do what you want.




回答3:


You can call expr to match strings against regular expressions from within Bourne Shell scripts. The below seems to work:

#!/bin/sh

var1="mtu eth0"

if [ "`expr \"$var1\" : \"mtu .*\"`" != "0" ];then
  echo "match"
fi



回答4:


I'd do the following:

# Removes anything but first word from "var1"
if [ "${var1%% *}" = "mtu" ] ; then ... fi

Or:

# Tries to remove the first word if it is "mtu", checks if we removed anything
if [ "${var1#mtu }" != "$var1" ] ; then ... fi



回答5:


I like to use the case statement to compare strings.

A trivial example is

case "$input"
in
  "$variable1") echo "matched the first value" 
     ;;
  "$variable2") echo "matched the second value"
     ;;
  *[a-z]*)  echo "input has letters" 
     ;;
  '')       echo "input is null!"
     ;;
   *[0-9]*)  echo "matched numbers (but I don't have letters, otherwise the letter test would have been hit first!)"
     ;;
   *) echo "Some wacky stuff in the input!"
esac

I've done crazy things like

case "$(cat file)"
in
  "$(cat other_file)")  echo "file and other_file are the same"
      ;;
  *)  echo "file and other_file are different"
esac

And that works too, with some limitations, such as the files can't be more than a couple megabytes and the shell simply doesn't see nulls, so if one file is full of nulls and the other has none, (and neither have anything else), this test won't see any difference between the two.

I don't use the file-comparing as a serious example, only an example of how the case statement is capable of doing much more flexible string matching than is available with test or expr or other similar shell expressions.




回答6:


In Bourne shell, if I want to check whether a string contains another string:

if [  `echo ${String} | grep -c ${Substr} ` -eq 1 ] ; then

Enclose echo ${String} | grep -c ${Substr} with two ` backticks:

To check whether the substring is at the beginning or at the end:

if [ `echo ${String} | grep -c "^${Substr}"` -eq 1 ] ; then
...
if [ `echo ${String} | grep -c "${Substr}$"` -eq 1 ] ; then



回答7:


Or, as an example of the =~ operator:

if [[ "$var1" =~ "mtu *" ]]


来源:https://stackoverflow.com/questions/183622/how-do-i-compare-strings-in-bourne-shell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!