Welcome on my blog

We use cookies to ensure you get the best experience on our website.

BASH – IF – Jak porównywać łańcuchy w Bashu

Autor: chmajster 26.08.2022

Operatory porównania są operatorami, które porównują wartości i zwracają TRUE lub FALSE. Podczas porównywania ciągów w Bashu możesz użyć następujących operatorów:

string1 != string2 – zwraca prawdę, jeśli stringi nie są równe.

string1 > string2 – zwraca prawdę, jeśli lewy stringi jest większy od prawego posortowanego w porządku leksykograficznym (alfabetycznym).

string1 < string2 – zwraca prawdę, jeśli prawy stringi jest większy od prawego posortowanego w porządku leksykograficznym (alfabetycznym).

-z string – zwraca prawdę, jeśli długość łańcucha jest zerowa.

-n string – zwraca prawdę, jeśli długość łańcucha jest niezerowa.

Sprawdź, czy dwa ciągi są równe.-z string

#!/bin/bash

VAR1="Linuxize"
VAR2="Linuxize"

if [ "$VAR1" = "$VAR2" ]; then
    echo "Strings are equal."
else
    echo "Strings are not equal."
fi

Sprawdź, czy łańcuch zawiera element.


VAR='GNU/Linux is an operating system'
if [[ $VAR == *"Linux"* ]]; then
  echo "It's there."
fi

Sprawdź, czy łańcuch jest pusty.

VAR=''
if [[ -z $VAR ]]; then
  echo "String is empty."
fi

Porównywanie DAT

# Porownywanie tych samych warunkow, nie korzystaj z "-" i innych znakow

todate=$(date -d 2013-07-18 +"%Y%m%d")  # = 20130718
cond=$(date -d 2013-07-15 +"%Y%m%d")    # = 20130715

if [ $todate -ge $cond ]; #put the loop where you need it
then
 echo 'yes';
fi

Sprawdzanie czy plik istnieje

FILE=/etc/resolv.conf
if [ -f "$FILE" ]; then
    echo "$FILE Istnieje."
else 
    echo "$FILE nie istnieje."
fi
#or#
[ -f /etc/resolv.conf ] && { echo "$FILE exist."; cp "$FILE" /tmp/; }

#File test operators

The test command includes the following FILE operators that allow you to test for particular types of files:

    -b FILE - True if the FILE exists and is a special block file.
    -c FILE - True if the FILE exists and is a special character file.
    -d FILE - True if the FILE exists and is a directory.
    -e FILE - True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).
    -f FILE - True if the FILE exists and is a regular file (not a directory or device).
    -G FILE - True if the FILE exists and has the same group as the user running the command.
    -h FILE - True if the FILE exists and is a symbolic link.
    -g FILE - True if the FILE exists and has set-group-id (sgid) flag set.
    -k FILE - True if the FILE exists and has a sticky bit flag set.
    -L FILE - True if the FILE exists and is a symbolic link.
    -O FILE - True if the FILE exists and is owned by the user running the command.
    -p FILE - True if the FILE exists and is a pipe.
    -r FILE - True if the FILE exists and is readable.
    -S FILE - True if the FILE exists and is a socket.
    -s FILE - True if the FILE exists and has nonzero size.
    -u FILE - True if the FILE exists, and set-user-id (suid) flag is set.
    -w FILE - True if the FILE exists and is writable.
    -x FILE - True if the FILE exists and is executable.

1 komentarz do „BASH – IF – Jak porównywać łańcuchy w Bashu”

  1. Warning: Undefined variable $comment in /usr/home/chmajster/domains/chmajster.usermd.net/public_html/wp-content/themes/Cryptmen.tk/functions.php on line 22
    Krzysztojm 26.08.2022
    iyu
Dodaj komentarz

Search
Categories
O Blogu

Cześć Podróżniku!

Ta strona ma nie być typowym poradnikiem w IT, Głównym jej cel to zapisanie krótkich notatek, które mogą się przydać w codziennym życiu podczas korzystania/konfiguracji różnych urządzeń np. Ustawienia DHCP na Routerze Cisco, Ustawieniu Karty sieciowej na Linuxie itp.

Buy me A coffee
[Empty]

Wszelkie prawa zastrzeżone