logo

„Bash Scripting“ – kaip patikrinti, ar failas egzistuoja

Šiame straipsnyje parašysime bash scenarijų, kad patikrintume, ar failai egzistuoja, ar ne.

Sintaksė:

  • testas [išraiška]
  • [ išraiška ]
  • [[ išraiška ]]

Čia, išraiška, mes rašome parametras ir failo pavadinimas . Pažiūrėkime kai kuriuos parametrus, kurie gali būti naudojami išraiškoje: –

  • f: Jis grąžina True, jei failas egzistuoja kaip įprastas (įprastas) failas.
  • -d: grąžina True, jei katalogas yra. -e : Grąžina True, jei yra kokio tipo failas. -c : Grąžina True, jei egzistuoja simbolių failas. -r : Grąžina True, jei yra skaitomas failas.
  • Į : Grąžina True, jei yra įrašomas failas .
  • -x : Grąžina True, jei yra vykdomasis failas. -p : Grąžina True, jei failas egzistuoja kaip vamzdis. -S : Grąžina True, jei failas egzistuoja kaip lizdas. -s : grąžina True, jei failas yra ir failo dydis nėra nulis. -L : Grąžina True, jei egzistuoja simbolinės nuorodos failas . -g : Grąžina True, jei failas egzistuoja ir nustatyta laikymo rinkinio grupės ID vėliavėlė. -G : t grąžina True, jei failas egzistuoja ir turi tą patį grupės ID, kuris yra apdorojamas. -k : Grąžina True, jei failas egzistuoja ir nustatyta lipni bitų vėliavėlė.

Dabar yra dar keli parametrai, skirti palyginti du failus.



    -ef: grąžina True, jei abu failai yra ir nurodo tą patį failą.

Pavyzdys :

FirstFile -ef SecondFile>
    -nt: grąžinama True, jei „FirstFile“ yra naujesnė nei „Secondfile“.

Pavyzdys :

bfs vs dfs
FirstFile -nt FileOld>
    -ot: grąžinama True, jei FirstFile yra senesnė nei SecondFile.

Pavyzdys:

FirstFile -ot SecondFile>

Paimkime keletą pavyzdžių, pagrįstų sintaksė:

    [ išraiška ]: Pirmiausia sukurkite failą pavadinimu FirstFile.sh ir parašykite jame šį scenarijų
#!/bin/bash # using [ expression ] syntax and in place # of File.txt you can write your file name if [ -f 'File.txt' ]; then # if file exist the it will be printed echo 'File is exist' else # is it is not exist then it will be printed echo 'File is not exist' fi>

Dabar išsaugokite ir paleiskite failą naudodami šią komandą

$ chmod +x ./FirstFile.sh $ ./FirstFile.sh>

Išvestis:

Išvestis

Pastaba: Kadangi failas.txt yra sistemoje. Taigi, atspausdintas failas egzistuoja.

    testas [išraiška]: dabar pakeiskite aukščiau pateiktą scenarijų FirstFile.sh taip
#!/bin/bash # using test expression syntax and in place # of File2.txt you can write your file name if test -f 'File2.txt' ; then # if file exist the it will be printed echo 'File is exist' else # is it is not exist then it will be printed echo 'File is not exist' fi>

Dabar dar kartą išsaugokite ir paleiskite failą naudodami šią komandą

$ chmod +x ./FirstFile.sh $ ./FirstFile.sh>

Išvestis:

Išvestis

Pastaba: Kadangi failo File2.txt sistemoje nėra. Taigi, atspausdintas failas neegzistuoja.

    [[ išraiška ]]: dar kartą pakeiskite aukščiau pateiktą scenarijų FirstFile.sh taip
#!/bin/bash # using [[ expression ]] syntax and in place # of File3.txt you can write your file name if test -f 'File3.txt' ; then # if file exist the it will be printed echo 'File is exist' else # is it is not exist then it will be printed echo 'File is not exist' fi>

Dabar dar kartą išsaugokite ir paleiskite failą naudodami šią komandą

 $ chmod +x ./FirstFile.sh $ ./FirstFile.sh>

Išvestis:

Išvestis

Pastaba: Kadangi failas File3.txt yra sistemoje. Taigi, atspausdintas failas egzistuoja.

palyginamas sąrašas

Pažiūrėkime pavyzdį, pagrįstą parametrais:

    Naudojant parametrą -d: sukurkite failą pavadinimu FirstDir.sh ir parašykite jame tokį scenarijų
!/bin/bash if [[ -d 'GFG_dir' ]] ; # Here GFG_dir is directory and in place of GFG_dir you can write your Directory name then echo 'Directory is exist' # If GFG_dir exist then it will be printed else echo 'Directory is not exist' # If GFG_dir is not exist then it will be printed fi>

Dabar išsaugokite ir paleiskite failą naudodami šią komandą

avl medžio sukimasis
$ chmod +x ./FirstDir.sh $ ./FirstDir.sh>

Išvestis:

Išvestis

Pastaba: Kadangi sistemoje yra GFG_dir. Taigi, išspausdintas katalogas egzistuoja.

Panašiai galite naudoti -f , -Tai yra , , -r , -c ir tt (pagal jų paskirtį) vietoj -d Norėdami patikrinti, ar yra įvairių tipų failų.

Pažiūrėkime pavyzdį, pagrįstą dviejų failų palyginimu:

  • Naudojant -nt parametras

Sukurkite failo pavadinimą Comparison_File.sh ir parašykite šį scenarijų

#!/bin/bash # New_file.txt and Old_File.txt are names of two files. if [[ 'New_File.txt' -nt 'Old_File.txt' ]] ; then # This will be printed if Condition is true echo 'New_File.txt is newer than Old_File.txt' else # This will be printed if Condition is False echo 'New_File.txt is not newer than Old_File.txt' fi>

Dabar išsaugokite ir paleiskite failą naudodami šią komandą

 $  chmod +x ./Comparison_File.sh $ ./Comparison_File.sh>

Išvestis:

Išvestis

Pastaba: Kadangi sistemoje yra abu failai ir New_File.txt yra naujesnis nei Old_File.txt

Pažiūrėkime pavyzdį Patikrinkite, ar failas neegzistuoja:

Sukurkite failą pavadinimu Check_Exist.sh ir parašykite jame tokį scenarijų

#!/bin/bash # using ! before -f parameter to check if # file does not exist if [[ ! -f 'GFG.txt' ]] ; then # This will printed if condition is True echo 'File is not exist' else # This will be printed if condition is False echo 'File is exist' fi>

Dabar išsaugokite ir paleiskite failą naudodami šią komandą

 $ chmod +x ./Check_Exist.sh $  ./Check_Exist.sh>

Išvestis:

Išvestis

Pastaba: GFG.txt sistemoje nėra. Taigi, jis išspausdins Failo nėra

Pažiūrėkime pavyzdį nenaudodami sąlygos If-else :

Sukurkite failą pavadinimu Geeks_File.sh ir parašykite jame šį scenarijų

#!/bin/bash # If File exist then first statement will be # printed and if it is not exist then 2nd # statement will be printed. [ -f 'GFG_File.txt' ] && echo 'File is exist' || echo 'File is not exist'>

Dabar išsaugokite ir paleiskite failą naudodami šią komandą

kiek savaičių yra per mėnesį
 $ chmod +x ./Geeks_File.sh $ ./Geeks_File.sh>

Išvestis:

Išvestis

Pastaba: Kadangi failas GFG_File.txt yra sistemoje. Taigi, atspausdintas failas egzistuoja.