Если файлы необходимы на каждой странице, поместите их в единственный файл. Это сократит количество Запроса HTTP и улучшит время отклика (для большого количества посещений).
См. лучшая практика Yahoo для других подсказок
case "$var" in
*\ * )
echo "match"
;;
*)
echo "no match"
;;
esac
[[ "$str" = "${str%[[:space:]]*}" ]] && echo "no spaces" || echo "has spaces"
You can use regular expressions in bash:
string="a b '' c '' d"
if [[ "$string" =~ \ |\' ]] # slightly more readable: if [[ "$string" =~ ( |\') ]]
then
echo "Matches"
else
echo "No matches"
fi
Edit:
For reasons obvious above, it's better to put the regex in a variable:
pattern=" |'"
if [[ $string =~ $pattern ]]
And quotes aren't necessary inside double square brackets. They can't be used on the right or the regex is changed to a literal string.
string="a b '' c '' d"
if [ "$string" == "${string//[\' ]/}" ]
then
echo did not contain space or single quote
else
echo did contain space or single quote
fi
The portable way to do this is with grep
:
S="a b '' c '' d"
if echo $S | grep -E '[ "]' >/dev/null
then
echo "It's a match"
fi
...a bit ugly, but guaranteed to work everywhere.
Как насчет подхода, подобного:
$ A="some string"; echo $A | grep \ | wc -l
1
$ A="somestring"; echo $A | grep \ | wc -l
0
?
function foo() {
echo "String: $*"
SPACES=$(($#-1))
echo "Spaces: $SPACES"
QUOTES=0
for i in $*; do
if [ "$i" == "'" ]; then
QUOTES=$((QUOTES+1))
fi
done
echo "Quotes: $QUOTES"
echo
}
S="string with spaces"
foo $S
S="single' 'quotes"
foo $S
S="single '' quotes"
foo $S
S="single ' ' quotes"
foo $S
возвращает:
String: string with spaces
Spaces: 2
Quotes: 0
String: single' 'quotes
Spaces: 1
Quotes: 0
String: single '' quotes
Spaces: 2
Quotes: 0
String: single ' ' quotes
Spaces: 3
Quotes: 2
Интересно, почему никто не упомянул набор [: space:]. Обычно вас интересует не только обнаружение космического персонажа. Мне часто нужно обнаружить любое пустое пространство, например TAB. Пример "grep" будет выглядеть так:
$ echo " " | egrep -q "[:space:]" && echo "Has no Whitespace" || echo "Has Whitespace"
Has Whitespace
$ echo "a" | egrep -q "[:space:]" && echo "Has no Whitespace" || echo "Has Whitespace"
Has no Whitespace