In dit artikel zullen we een bash-script schrijven om te controleren of bestanden bestaan of niet.
Syntaxis:
- testen [uitdrukking]
- [ uitdrukking ]
- [[ uitdrukking ]]
Hier schrijven we in uitdrukking parameter En bestandsnaam . Laten we enkele parameters bekijken die in de uitdrukking kunnen worden gebruikt: –
- – F: Het retourneert True als het bestand bestaat als een algemeen (normaal) bestand. -d: het retourneert True als de map bestaat. -e: Het retourneert True als er een type bestand bestaat. -c: Het retourneert True als het tekenbestand bestaat. -r: Het retourneert True als er een leesbaar bestand bestaat.
- – In : Het retourneert True als er een beschrijfbaar bestand bestaat . -x: Het retourneert True als er een uitvoerbaar bestand bestaat. -p: Het retourneert True als het bestand als pipe bestaat. -S: Het retourneert True als het bestand als socket bestaat. -s: het retourneert True als een bestand bestaat en de grootte van het bestand niet nul is. -L: Het retourneert True als het bestand met de symbolische link bestaat . -g: Het retourneert True als het bestand bestaat en de hold set group id-vlag is ingesteld.. -G: I t retourneert True als het bestand bestaat en dezelfde groeps-ID bevat die wordt verwerkt. -k: Het retourneert True als het bestand bestaat en de sticky bit-vlag is ingesteld.
Nu zijn er nog enkele parameters voor vergelijking tussen de twee bestanden.
- -ef: Het retourneert True als beide bestanden bestaan en hetzelfde bestand aangeven.
Voorbeeld :
FirstFile -ef SecondFile>
- -nt: Het retourneert True als FirstFile nieuwer is dan Secondfile.
Voorbeeld :
nginx
FirstFile -nt FileOld>
- -ot: Het retourneert True als FirstFile ouder is dan SecondFile.
Voorbeeld:
FirstFile -ot SecondFile>
Laten we enkele voorbeelden nemen op basis van syntaxis:
- [expressie]: Maak eerst een bestand met de naam FirstFile.sh en schrijf het volgende script erop
#!/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>
Sla het bestand nu op en voer het uit met behulp van de volgende opdracht
$ chmod +x ./FirstFile.sh $ ./FirstFile.sh>
Uitgang:

Uitvoer
Opmerking: Omdat File.txt aanwezig is in het systeem. Het afgedrukte bestand bestaat dus.
- test [expressie]: Wijzig nu het bovenstaande script in FirstFile.sh als volgt
#!/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>
Sla het bestand nu opnieuw op en voer het uit met de volgende opdracht
$ chmod +x ./FirstFile.sh $ ./FirstFile.sh>
Uitgang:

Uitvoer
Opmerking: Omdat File2.txt niet aanwezig is in het systeem. Er werd dus afgedrukt Bestand bestaat niet.
- [[ expressie ]]: Wijzig het bovenstaande script opnieuw in FirstFile.sh als volgt
#!/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>
Sla het bestand nu opnieuw op en voer het uit met de volgende opdracht
$ chmod +x ./FirstFile.sh $ ./FirstFile.sh>
Uitgang:
10 ml tot oz

Uitvoer
Opmerking: Omdat File3.txt aanwezig is in het systeem. Het werd dus afgedrukt Bestand is bestaan .
Laten we een voorbeeld bekijken op basis van parameters:
- Gebruik de parameter -d: Maak een bestand met de naam FirstDir.sh en schrijf daarin het volgende script
!/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>
Sla nu het bestand op en voer het uit met de volgende opdracht
voorbeeld van gebruikersnaam
$ chmod +x ./FirstDir.sh $ ./FirstDir.sh>
Uitgang:

Uitvoer
Opmerking: Omdat de GFG_dir aanwezig is in het systeem. Het werd dus afgedrukt Directory is exist .
Op dezelfde manier kunt u gebruiken -F , -Het is , -In , -R , -C ,enz. (volgens hun gebruik) in plaats van -D voor het controleren van het bestaan van verschillende soorten bestanden.
Laten we een voorbeeld bekijken gebaseerd op een vergelijking van twee bestanden:
- Gebruik makend van -nt parameter
Maak een bestandsnaam Comparison_File.sh en schrijf het volgende script
#!/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>
Sla nu het bestand op en voer het uit met de volgende opdracht
$ chmod +x ./Comparison_File.sh $ ./Comparison_File.sh>
Uitgang:

Uitvoer
Opmerking: Omdat beide bestanden aanwezig zijn in het systeem en New_File.txt nieuwer is dan Old_File.txt
Laten we het voorbeeld bekijken Controleer of bestand niet bestaat:
Maak een bestand met de naam Check_Exist.sh en schrijf daarin het volgende script
#!/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>
Sla nu het bestand op en voer het uit met de volgende opdracht
$ chmod +x ./Check_Exist.sh $ ./Check_Exist.sh>
Uitgang:

Uitvoer
Opmerking: GFG.txt is niet aanwezig in het systeem. Er wordt dus afgedrukt Bestand bestaat niet
Laten we een voorbeeld bekijken zonder de If-else-voorwaarde te gebruiken:
Maak een bestand met de naam Geeks_File.sh en schrijf daarin het volgende script
#!/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'>
Sla nu het bestand op en voer het uit met de volgende opdracht
$ chmod +x ./Geeks_File.sh $ ./Geeks_File.sh>
Uitgang:
Java-ontwerppatroon

Uitvoer
Opmerking: Omdat de GFG_File.txt aanwezig is in het systeem. Het werd dus afgedrukt Bestand is bestaan .