Zum Inhalt springen

Empfohlene Beiträge

Geschrieben

Hallo,

folgende Frage wird für euch UNIX-Gurus sicherlich leicht zu beantworten sein:

warum verwendet man hier Anführungszeichen

if [ "$success" -eq 0 ]

hier aber nicht:

if [ $yesno != "y" ]

ist es so, dass man beim Vergleich von Zahlen (der ja mit -eq stattfindet) die Variable immer in Anführungszeichen einschließen muss - oder hat es einen anderen Grund?

Gruß,

Technician

P.S.: Ich habe derzeit keinen UNIX-Zugriff, deshalb meine Frage.

Ansonsten wäre es sicherlich ein leichtes, das einfach auszuprobieren!

Geschrieben

Ich bin zwar auch nur ein Linux Newbie, aber hab etwas gefunden. (Google - mehr sag ich nicht ;) )

using Quotes to enclose your variables

Sometimes, it is a good idea to protect variable names in double quotes. This is usually the most important if your variables value either (a) contains spaces or (B) is the empty string. An example is as follows:

#!/bin/bash

X=""

if [ -n $X ]; then # -n tests to see if the argument is non empty

echo "the variable X is not the empty string"

fi

This script will give the following output:

the variable X is not the empty string

Why ? because the shell expands $X to the empty string. The expression [ -n ] returns true (since it is not provided with an argument). A better script would have been:

#!/bin/bash

X=""

if [ -n "$X" ]; then # -n tests to see if the argument is non empty

echo "the variable X is not the empty string"

fi

In this example, the expression expands to [ -n "" ] which returns false, since the string enclosed in inverted commas is clearly empty.

http://pegasus.rutgers.edu/~elflord/unix/bash-tute.html

Erstelle ein Benutzerkonto oder melde Dich an, um zu kommentieren

Du musst ein Benutzerkonto haben, um einen Kommentar verfassen zu können

Benutzerkonto erstellen

Neues Benutzerkonto für unsere Community erstellen. Es ist einfach!

Neues Benutzerkonto erstellen

Anmelden

Du hast bereits ein Benutzerkonto? Melde Dich hier an.

Jetzt anmelden

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...