Linux Scripting

Linux Scripting

Post by DJohnso » Tue, 12 Dec 2000 08:30:08



I wrote a scipt in Linux and either I don't know how to get it to execute
or I've written it wrong.  The script is below. Thanks
count=0
for i in `ls -l`
do
if [ -d $i ]
then
count=`exp ^ $count +1`
fi
done
echo $count

--
Posted via CNET Help.com
http://www.help.com/

 
 
 

Linux Scripting

Post by Scoote » Tue, 12 Dec 2000 08:47:46



> I wrote a scipt in Linux and either I don't know how to get it to execute
> or I've written it wrong.  The script is below. Thanks
> count=0
> for i in `ls -l`
> do
> if [ -d $i ]
> then
> count=`exp ^ $count +1`
> fi
> done
> echo $count

Will it not run, or are you getting error messages from the shell when you
try to run it?

If it is the former, you most likely need to give the file executable
permissions using chmod.

As far as errors it might generate, I am not familiar with the syntax you
are using here:
    count=`exp ^ $count +1`
It looks like you are trying to increase the value of count by one here; it
that's the case try altering that line to read like this:
    count=`expr $count + 1`
The whitespace between the + sign and the number "1" matters too.

HTH

 
 
 

Linux Scripting

Post by Neil Cher » Tue, 12 Dec 2000 09:22:30



>I wrote a scipt in Linux and either I don't know how to get it to execute
>or I've written it wrong.  The script is below. Thanks
>count=0
>for i in `ls -l`
>do
>if [ -d $i ]
>then
>count=`exp ^ $count +1`
>fi
>done
>echo $count

I'll assume you want to count directories, first the "if [ -d $i ]"
needs a " ; " (not the space before, I like to put the then after it
so I add another space). Next I have no idea what exp is. If it's expr
then you'll need to man page it and see what else you're doing wrong.

--

http://members.home.net/ncherry                         (Text only)
http://meltingpot.fortunecity.com/lightsey/52           (Graphics)
http://linuxha.sourceforge.net/                             (SourceForge)

 
 
 

Linux Scripting

Post by Floyd Davidso » Tue, 12 Dec 2000 14:54:21




>>I wrote a scipt in Linux and either I don't know how to get it to execute
>>or I've written it wrong.  The script is below. Thanks
>>count=0
>>for i in `ls -l`
>>do
>>if [ -d $i ]
>>then
>>count=`exp ^ $count +1`
>>fi
>>done
>>echo $count

>I'll assume you want to count directories, first the "if [ -d $i ]"
>needs a " ; " (not the space before, I like to put the then after it

No, it does not need a ; because there is a newline between the
conditional and the "then".   This,

  if [ conditional ] ; then expression; fi

is the same as,

  if [ conditional ]
  then
    expresssion
  fi

(Note the two semicolons are not in the second construct.)

However, the for loop will not work because "ls -l" produces an
entire line of output, not just a single filename.  First, the
"-1" option instead of the "-l" option should be used; and
second, the full pathname for "ls" should be given because "ls"
is often an alias.

Quote:>so I add another space). Next I have no idea what exp is. If it's expr
>then you'll need to man page it and see what else you're doing wrong.

A good guess is that it is indeed expr, and should be

  count=`expr $count + 1`

Hence, the entire script could be:

#!/bin/sh
#
count=0
for i in `/bin/ls -1`
do
  if [ -d $i ]
  then
    count=`expr $count + 1`
  fi
done
echo $count

--
Floyd L. Davidson         <http://www.ptialaska.net/~floyd>

 
 
 

Linux Scripting

Post by Harlan Grov » Tue, 12 Dec 2000 17:23:15




<snip>

Quote:>However, the for loop will not work because "ls -l" produces an
>entire line of output, not just a single filename.  First, the
>"-1" option instead of the "-l" option should be used; and
>second, the full pathname for "ls" should be given because "ls"
>is often an alias.

ls may be an alias using default comand line switches, but /bin/ls is a
required POSIX binary. As long as /bin is in PATH and plain ls behavior
is assumed, ls may be used without its full pathname.

<snip>

Quote:>#!/bin/sh
>#
>count=0
>for i in `/bin/ls -1`
>do
>  if [ -d $i ]
>  then
>    count=`expr $count + 1`
>  fi
>done
>echo $count

If the standard linux utilities are present, simplify:

#!/bin/sh
ls -Al | grep '^d' | wc -l

which also avoids nastiness if the listing of all subdirectories would
exceed the shell's maximum line length.

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

Linux Scripting

Post by Jimbo » Tue, 12 Dec 2000 19:54:38



> I wrote a scipt in Linux and either I don't know how to get it to execute
> or I've written it wrong.  The script is below. Thanks
> count=0
> for i in `ls -l`
> do
> if [ -d $i ]
> then
> count=`exp ^ $count +1`
> fi
> done
> echo $count
> --
> Posted via CNET Help.com
> http://www.help.com/

At the top of all scripts you need to specify the shell that the script will be run in:

#!/bin/bash

or whatever shell you use.

Jimbob
--
****************************************************
*                                                  *
*   "Sudden success in golf is like the sudden     *
*    acquisition of wealth. It is apt to unsettle  *
*    and deteriorate the character"                *
*                                                  *
****************************************************

 
 
 

Linux Scripting

Post by Floyd Davidso » Tue, 12 Dec 2000 20:13:29




>> I wrote a scipt in Linux and either I don't know how to get it to execute
>> or I've written it wrong.  The script is below. Thanks
>> count=0
>> for i in `ls -l`
>> do
>> if [ -d $i ]
>> then
>> count=`exp ^ $count +1`
>> fi
>> done
>> echo $count

>> --
>> Posted via CNET Help.com
>> http://www.help.com/

>At the top of all scripts you need to specify the shell that
>the script will be run in:

>#!/bin/bash

>or whatever shell you use.

>Jimbob

It is probably a _good_ idea to put it there, but it is _not_
a requirement.

However, to run a script using other than the command line shell,
either that mechanism is in place or the shell must be specified
on the command line:

  sh -c script

for example, unless of course the command line shell is the
desired shell (which is usually the case with bash, as was true
in the above instance).

--
Floyd L. Davidson         <http://www.ptialaska.net/~floyd>

 
 
 

Linux Scripting

Post by c.. » Wed, 13 Dec 2000 07:26:44


I assume that you flagged the file as an executable?
chmod 700 /filename
or
chmod 777 /filename





>>> I wrote a scipt in Linux and either I don't know how to get it to execute
>>> or I've written it wrong.  The script is below. Thanks
>>> count=0
>>> for i in `ls -l`
>>> do
>>> if [ -d $i ]
>>> then
>>> count=`exp ^ $count +1`
>>> fi
>>> done
>>> echo $count

>>> --
>>> Posted via CNET Help.com
>>> http://www.help.com/

>>At the top of all scripts you need to specify the shell that
>>the script will be run in:

>>#!/bin/bash

>>or whatever shell you use.

>>Jimbob

>It is probably a _good_ idea to put it there, but it is _not_
>a requirement.

>However, to run a script using other than the command line shell,
>either that mechanism is in place or the shell must be specified
>on the command line:

>  sh -c script

>for example, unless of course the command line shell is the
>desired shell (which is usually the case with bash, as was true
>in the above instance).

 
 
 

1. oops script for /usr/src/linux/scripts

I made a simple bug_report.sh script to facilitate
generating a bug report.  It could be included in
/usr/src/linux/scripts and could be referenced in
/usr/src/linux/BUG-REPORTING.

If anyone is interested, please see attached.

Cheers,
--

  bug_report.sh
2K Download

2. PPP to ISP WORKS, BUT NOT TO SCHOOL--HELP PLEASE!!!

3. Help with linux script

4. [PATCH] x86 32bit entry point support...

5. Passing Value to Linux Script from CGI

6. dis command for AIX 4.3?

7. linux script from windows

8. Q: Enlightenment WM

9. Documentation on Linux scripts ?

10. need win95/linux script

11. Need I a Linux Script?

12. Linux Script Help Please?

13. perl or native linux scripting tools