[Test Script] Test file extension
korian
-
korian -
korian -
Hello,
I’m a beginner so please be lenient svp.
I’m walking through a directory in a loop and retrieving the file names one by one into a variable i.
How to test that i contains the txt extension?
I’m in sh shell.
Thanks in advance for the answers.
I’m a beginner so please be lenient svp.
I’m walking through a directory in a loop and retrieving the file names one by one into a variable i.
How to test that i contains the txt extension?
I’m in sh shell.
#!/bin/sh # Variable declarations NbFic=0 NbFicTxt=0 # Loop over all files (ignore errors) for i in `find . -type f -name '*' 2>/dev/null` do # Count all non-directory files NbFic=`expr $NbFic + 1`; if ???????????? $i et txt ????????; then # Count all .txt files NbFicTxt=`expr $NbFicTxt + 1`; fi done
Thanks in advance for the answers.
Configuration: Windows XP Safari 532.0
6 answers
Hi,
So:
;-))
--
$ man woman
There is no manual page for woman.
[tmpfs]$ var="fichier.txt" [tmpfs]$ echo ${var} fichier.txt [tmpfs]$ echo ${var##*.} txt [tmpfs]$ So:
if [ "${var##*.}" = "txt" ]; then... ;-))
--
$ man woman
There is no manual page for woman.
Hi,
# Count all the .txt files
And like this, isn’t it good?
--
106485010510997108
# Count all the .txt files
And like this, isn’t it good?
$ nbrtxt=$(find ./trash2 -type f -name '*.txt' 2>/dev/null| wc -l) $ echo $nbrtxt 6
--
106485010510997108
Thank you for your quick responses, I appreciate it. I had to let the weekend pass to test them :)
@jipicy: the chain triggers an error: incorrect substitution. Are you sure about the syntax for a sh shell?
@lami20j: I want to perform a processing on each file and a particular one on the txt files and not just count them.
@jipicy: the chain triggers an error: incorrect substitution. Are you sure about the syntax for a sh shell?
@lami20j: I want to perform a processing on each file and a particular one on the txt files and not just count them.
Re-
Normally there should not be any problem ;-
For the lami20j command, you just need to add the parameter "-exec" followed by the appropriate command... for example:
find ./trash2 -type f -name '*.txt' -exec stat -c '%n : %A' {} \;
or also with "xargs":
find ./trash2 -type f -name '*.txt' | xargs stat-c '%n : %AWhat kind of processing should you execute on each file? -- $ man woman There is no man page for woman.
Hi there,
How does your mask work? because I can’t find any example in my book on scripts. The wildcards are * or ? for me and apparently there’s no use of {}.
As for the treatments, I’d like to do a grep and a sed.
Thanks for the help.
How does your mask work? because I can’t find any example in my book on scripts. The wildcards are * or ? for me and apparently there’s no use of {}.
As for the treatments, I’d like to do a grep and a sed.
Thanks for the help.
Open the man page for sh and search for "Parameter Expansion":
Quand a ton traitement, l'expression initiée par lami20j me parait la plus adaptée en remplaçant "wc" par "sed" (qui regroupe les fonctionnalités de "grep")...
--
$ man woman
Il n'y a pas de page de manuel pour woman.
Parameter Expansion The format for parameter expansion is as follows: ${expression} where expression consists of all characters until the matching `}'. Any `}' escaped by a backslash or within a quoted string, and characters in embedded arithmetic expansions, command substitutions, and variable expansions, are not examined in determining the matching `}'. The simplest form for parameter expansion is: ${parameter} The value, if any, of parameter is substituted. The parameter name or symbol can be enclosed in braces, which are optional except for positional parameters with more than one digit or when parameter is followed by a character that could be interpreted as part of the name. If a parameter expansion occurs inside double-quotes: 1. Pathname expansion is not performed on the results of the expansion. 2. Field splitting is not performed on the results of the expansion, with the exception of the special parameter @. In addition, a parameter expansion can be modified by using one of the following formats. ${parameter:-word} Use Default Values. If parameter is unset or null, the expansion of word is substituted; otherwise, the value of parameter is sub- stituted. ${parameter:=word} Assign Default Values. If parameter is unset or null, the expan- sion of word is assigned to parameter. In all cases, the final value of parameter is substituted. Only variables, not posi- tional parameters or special parameters, can be assigned in this way. ${parameter:?[word]} Indicate Error if Null or Unset. If parameter is unset or null, the expansion of word (or a message indicating it is unset if word is omitted) is written to standard error and the shell exits with a nonzero exit status. Otherwise, the value of parameter is substituted. An interactive shell need not exit. ${parameter:+word} Use Alternate Value. If parameter is unset or null, null is sub- stituted; otherwise, the expansion of word is substituted. In the parameter expansions shown previously, use of the colon in the format results in a test for a parameter that is unset or null; omission of the colon results in a test for a parameter that is only unset. ${#parameter} String Length. The length in characters of the value of parameter. The following four varieties of parameter expansion provide for substring processing. In each case, pattern matching notation (see Shell Patterns), rather than regular expression notation, is used to evaluate the patterns. If parameter is one of the special parameters * or @, the result of the expansion is unspecified. Enclosing the full parameter expansion string in double-quotes does not cause the following four vari- eties of pattern characters to be quoted, whereas quoting characters within the braces has this effect. ${parameter%word} Remove Smallest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest portion of the suffix matched by the pattern deleted. ${parameter%%word} Remove Largest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest portion of the suffix matched by the pattern deleted. ${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest portion of the prefix matched by the pattern deleted. ${parameter##word} Remove Largest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest portion of the prefix matched by the pattern deleted. Quand a ton traitement, l'expression initiée par lami20j me parait la plus adaptée en remplaçant "wc" par "sed" (qui regroupe les fonctionnalités de "grep")...
--
$ man woman
Il n'y a pas de page de manuel pour woman.
Ok thanks for the manual extraction :) This is where I see I still don't have the right Unix reflexes.
Lami20j's solution indeed seems more effective by working on sets rather than a review 1 by 1, but I wanted to do a nesting of if fi to test. So I want first to get it working with test and then I would modify it to optimize.
I will continue with your ## and I will keep you posted.
Thanks for the help anyway.
Lami20j's solution indeed seems more effective by working on sets rather than a review 1 by 1, but I wanted to do a nesting of if fi to test. So I want first to get it working with test and then I would modify it to optimize.
I will continue with your ## and I will keep you posted.
Thanks for the help anyway.