Jeg har et shell-script, hvor vi har følgende linjer if [ -z "$xyz" ]
og if [ -n "$abc" ]
, men jeg er ikke sikker på, hvad deres formål er. Kan nogen venligst forklare?
Svar
Du kan finde en meget flot reference til bashs operatører her . Hvis du bruger en anden skal, skal du bare søge efter <my shell> operators
, så finder du alt hvad du har brug for. I dit særlige tilfælde er du ved hjælp af:
-n string is not null. -z string is null, that is, has zero length
For at illustrere:
$ foo="bar"; $ [ -n "$foo" ] && echo "foo is not null" foo is not null $ [ -z "$foo" ] && echo "foo is null" $ foo=""; $ [ -n "$foo" ] && echo "foo is not null" $ [ -z "$foo" ] && echo "foo is null" foo is null
Kommentarer
Svar
man test
eller man [
giver dig alle mulighederne for at teste kommandoen. I dette tilfælde tester -n for at se, om indholdet af $ abc har en ikke-nul længde, og -z tester for at se, om indholdet af $ xyz er en nul-længdestreng.
Kommentarer
- mand [fungerer ikke ' fungerer ikke for mig i GNU bash, version 4.1.2 (1) -release (x86_64- redhat-linux-gnu). Men +1 for mandtest.
- Bemærk
man test
(altid?) Giver mandsiden til den eksterne programversion, som (for GNU-coreutils version på mindst) advarer eksplicit, at nogle (IME mest) skaller har en indbygget version, der kan være anderledes.
Svar
For at udvide terdons svar fandt jeg, at Unix / Linux – Shell Basic Operators på Tutorials Point inkluderer også filrelaterede operatører (såvel som andre nyttige).
-b file Checks if file is a block special file; if yes, then the condition becomes true. [ -b $file ] is false. -c file Checks if file is a character special file; if yes, then the condition becomes true. [ -c $file ] is false. -d file Checks if file is a directory; if yes, then the condition becomes true. [ -d $file ] is not true. -f file Checks if file is an ordinary file as opposed to a directory or special file; if yes, then the condition becomes true. [ -f $file ] is true. -g file Checks if file has its set group ID (SGID) bit set; if yes, then the condition becomes true. [ -g $file ] is false. -k file Checks if file has its sticky bit set; if yes, then the condition becomes true. [ -k $file ] is false. -p file Checks if file is a named pipe; if yes, then the condition becomes true. [ -p $file ] is false. -t file Checks if file descriptor is open and associated with a terminal; if yes, then the condition becomes true. [ -t $file ] is false. -u file Checks if file has its Set User ID (SUID) bit set; if yes, then the condition becomes true. [ -u $file ] is false. -r file Checks if file is readable; if yes, then the condition becomes true. [ -r $file ] is true. -w file Checks if file is writable; if yes, then the condition becomes true. [ -w $file ] is true. -x file Checks if file is executable; if yes, then the condition becomes true. [ -x $file ] is true. -s file Checks if file has size greater than 0; if yes, then condition becomes true. [ -s $file ] is true. -e file Checks if file exists; is true even if file is a directory but exists. [ -e $file ] is true.
-n
til at fungere, kan det skyldes, at du følger nogle dårlige guider på nettet (f.eks. GeeksforGeeks eller TutorialsPoint ) som ikke citerer variablerne. Dette svar, og guiden der er linket her, citerer korrekt Hvis du bruger-n
uden at citere, vil det fortælle dig, at det ' ikke er tomt, selv når det er! @terdon, mange tak!