|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Please bear with me onthis stupid question.
I have a bash script with a line: <code> w3flag=$(( $(ls *.ps | wc -w) == 18 )) </code> The problem is when there are no files matching the pattern *.ps the code works but prints out an error message. How can I either: 1) suppress the error message for this line or 2) short circuit this test if there are no files matching *.ps Thanks |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Nov 9, 4:11 pm, Brendan <brendandetra...@yahoo.com> wrote:
> Please bear with me onthis stupid question. > I have a bash script with a line: > > <code> > w3flag=$(( $(ls *.ps | wc -w) == 18 )) > </code> w3flag=$(( $(ls *.ps 2>/dev/null | wc -w) == 18 )) |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Brendan <brendandetracey@yahoo.com> wrote:
> Please bear with me onthis stupid question. > I have a bash script with a line: > > <code> > w3flag=$(( $(ls *.ps | wc -w) == 18 )) > </code> > > The problem is when there are no files matching the pattern *.ps the > code works but prints out an error message. > > How can I either: > 1) suppress the error message for this line > or > 2) short circuit this test if there are no files matching *.ps A simple solution is to redirect standard error of ls to /dev/null: w3flag=$(( $(ls *.ps 2>/dev/null | wc -w) == 1 )) -- Kenan Kalajdzic |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On 2007-11-09, Brendan wrote:
> > Please bear with me onthis stupid question. > I have a bash script with a line: > ><code> > w3flag=$(( $(ls *.ps | wc -w) == 18 )) ></code> > > The problem is when there are no files matching the pattern *.ps the > code works but prints out an error message. > > How can I either: > 1) suppress the error message for this line > or > 2) short circuit this test if there are no files matching *.ps You don't need either ls or wc: set -- *.ps if [ -f "$1" ] then w3flag=$# else w3flag=0 fi Special cases may need a slightly more elaborate test. -- Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Nov 10, 4:18 am, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:
> On 2007-11-09, Brendan wrote: > > > Please bear with me onthis stupid question. > > I have a bash script with a line: > > ><code> > > w3flag=$(( $(ls *.ps | wc -w) == 18 )) > ></code> > > > The problem is when there are no files matching the pattern *.ps the > > code works but prints out an error message. > > > How can I either: > > 1) suppress the error message for this line > > or > > 2) short circuit this test if there are no files matching *.ps > > You don't need either ls or wc: > > set -- *.ps > if [ -f "$1" ] > then > w3flag=$# > else > w3flag=0 > fi > > Special cases may need a slightly more elaborate test. > > -- > Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/> > Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) > ===== My code in this post, if any, assumes the POSIX locale > ===== and is released under the GNU General Public Licence- Hide quoted text - > > - Show quoted text - I do not understand this one. The output will be either 0 or the number of files matching the pattern? And why does the $1 not refer to the first argument passed into the script, but instead to the argument of the set command? |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 2007-11-11, Brendan wrote:
> > On Nov 10, 4:18 am, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote: >> On 2007-11-09, Brendan wrote: >> >> > Please bear with me onthis stupid question. >> > I have a bash script with a line: >> >> ><code> >> > w3flag=$(( $(ls *.ps | wc -w) == 18 )) >> ></code> >> >> > The problem is when there are no files matching the pattern *.ps the >> > code works but prints out an error message. >> >> > How can I either: >> > 1) suppress the error message for this line >> > or >> > 2) short circuit this test if there are no files matching *.ps >> >> You don't need either ls or wc: >> >> set -- *.ps >> if [ -f "$1" ] >> then >> w3flag=$# >> else >> w3flag=0 >> fi >> >> Special cases may need a slightly more elaborate test. > > I do not understand this one. The output will be either 0 or the > number of files matching the pattern? There is no output in that snippet. The number of files matching the pattern will be stored in $w3flag. > And why does the $1 not refer to the first argument passed into the > script, but instead to the argument of the set command? Once any options are used up, the remaining arguments to set replace the positional parameters. If you want to use the previous positional parameters, save them into meaningful variables before using set. -- Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== and is released under the GNU General Public Licence |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
> > I do not understand this one. The output will be either 0 or the
> > number of files matching the pattern? > > There is no output in that snippet. The number of files matching > the pattern will be stored in $w3flag. > That's what I meant by "output". Thanks for the . I'll have to get more familiar with the set command. |
|
![]() |
| Outils de la discussion | |
|
|