Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Multiple Files in Shell testen?
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German)
View previous topic :: View next topic  
Author Message
kami22
Tux's lil' helper
Tux's lil' helper


Joined: 14 Apr 2006
Posts: 116

PostPosted: Mon Mar 22, 2010 6:48 am    Post subject: Multiple Files in Shell testen? Reply with quote

Hi,

kurze Frage zu shell:
Wenn ich checken will ob ein Filetyp in einem Verzeichnis existiert wie mache ich das? if [ -f /home/kami/*.txt ]; then hat das problem das es nicht geht wenn es mehrere txt dateien in dem verzeichnis gibt.

Gruß kami
Back to top
View user's profile Send private message
Finswimmer
Bodhisattva
Bodhisattva


Joined: 02 Sep 2004
Posts: 5467
Location: Langen (Hessen), Germany

PostPosted: Mon Mar 22, 2010 7:12 am    Post subject: Reply with quote

[ $(ls /tmp/*.tx2342t 2>/dev/null|wc -l) -gt 0 ] && echo true

wäre eine Möglichkeit.
Oder du versuchst es mit find.

Ich denke, da gibt es mehrere Möglichkeiten.

Gruß
Tobi
_________________
Bitte auf Rechtschreibung, korrekte Formatierung und Höflichkeit achten!
Danke
Back to top
View user's profile Send private message
slick
Bodhisattva
Bodhisattva


Joined: 20 Apr 2003
Posts: 3495

PostPosted: Mon Mar 22, 2010 10:31 am    Post subject: Reply with quote

(alle ungetestet)

find

Code:
if [ "`find /pfad -maxdepth 1 -type f -iname '*.txt' 2> /dev/null`" == "" ] ; then ...


ls

Code:
if [ "`ls -1 /pfad/*.txt 2> /dev/null`" == "" ] ; then ...


ls & grep

Code:
if ls -1 /pfad/ | grep ".txt$" &> /dev/null ; then ...
Back to top
View user's profile Send private message
69719
l33t
l33t


Joined: 20 Sep 2004
Posts: 865

PostPosted: Mon Mar 22, 2010 11:52 am    Post subject: Reply with quote

Oder auch
Code:

ls *.dlc &>/dev/null && {
   echo "datei existiert."
   ...
}
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9625
Location: beyond the rim

PostPosted: Mon Mar 22, 2010 12:45 pm    Post subject: Reply with quote

Noch ne Variante ohne externe Programme:
Code:
if [ "$(echo *.txt)" != "*.txt" ]; then
    # Textdatei vorhanden
else
    # keine Textdatei vorhanden
fi
Back to top
View user's profile Send private message
kami22
Tux's lil' helper
Tux's lil' helper


Joined: 14 Apr 2006
Posts: 116

PostPosted: Mon Mar 22, 2010 6:39 pm    Post subject: Reply with quote

Ich habe jetzt folgende Variante: if $(ls /home/kami/*.txt | grep -q 'txt'); then

wie findet ihr die?

Gruß kami
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9625
Location: beyond the rim

PostPosted: Mon Mar 22, 2010 10:27 pm    Post subject: Reply with quote

kami22 wrote:
Ich habe jetzt folgende Variante: if $(ls /home/kami/*.txt | grep -q 'txt'); then

wie findet ihr die?

Aus mehreren Gründen schlecht:
1) `grep 'txt'` würde z.B. auch bei test-txt.sh zutreffen
2) weiss jetzt nicht ob `if $(command); then` legal ist (insbesondere ohne Quotes), würde ich aber zumindest als schlechten Stil ankreiden. Entweder `if command; then` oder `if [ -n "$(command)" ]; then`
3) ist jetzt sehr subjektiv, aber mMn hat `ls`in Skripten nix verloren.
Back to top
View user's profile Send private message
kami22
Tux's lil' helper
Tux's lil' helper


Joined: 14 Apr 2006
Posts: 116

PostPosted: Tue Mar 23, 2010 6:04 am    Post subject: Reply with quote

Hi,

okay danke Genone.

Dann nehme ich deine Variante.

Gruß kami
Back to top
View user's profile Send private message
schachti
Advocate
Advocate


Joined: 28 Jul 2003
Posts: 3765
Location: Gifhorn, Germany

PostPosted: Tue Mar 23, 2010 6:30 am    Post subject: Reply with quote

Genone wrote:
3) ist jetzt sehr subjektiv, aber mMn hat `ls`in Skripten nix verloren.


Warum das?
_________________
Never argue with an idiot. He brings you down to his level, then beats you with experience.

How-To: Daten verschlüsselt auf DVD speichern.
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9625
Location: beyond the rim

PostPosted: Tue Mar 23, 2010 1:06 pm    Post subject: Reply with quote

schachti wrote:
Genone wrote:
3) ist jetzt sehr subjektiv, aber mMn hat `ls`in Skripten nix verloren.


Warum das?

Weils eigentlich immer ne bessere Option gibt. Um nur Dateinamen aufzulisten reicht die Shell Expansion alleine aus, um detaillierte Dateiinformationen zu bekommen gitb es `stat`, und für kompliziertere Aufgaben kann man `find` nehmen. Und wenn man sich das Ausgabeformat von `ls -l` ansieht merkt man schnell dass es nicht sonderlich skriptfreundlich ist.

Ist aber wie gesagt meine sehr subjektive Ansicht.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum