Forum Home
Press F1
 
Thread ID: 34297 2003-06-09 06:30:00 Linux bash scripting, need help ......!! dkbaj (1047) Press F1
Post ID Timestamp Content User
151203 2003-06-09 06:30:00 I have been asked to wirte a bash script to display the following as the output :

>filestats /bin /etc /var /etc/password /usr/sbin

/bin : 0 ordinary, 86 executable, 0 directories, 15 links.
/etc : 144 ordinary, 6 executable, 65 directories, 15 links.
/var : 0 ordinary, 0 executable, 19 directories, 1 links.
/etc/passwd : is not a directory
/usr/sbin : 15 ordinary, 232 executable, 0 directories, 49 links.

I have gotten as far as this with the script:

clear

if [ $# ieq 0 ]

then
DirList = "$PWD"elif
DirList = "$*"


for
Dir in $DirList do
Dir ls $Dir


for
File in 'ls $Dir 'do

if[ -f $File ]
then files=$(($files + 1))
if [ -x $File ]
then execF=$(($execF + 1))
if [ -d $File ]
then dire=$(($dire + 1))
fi


fi

done


echo: "numfiles:"$files
echo: "numexecF":$execF
echo: "numexecF":$execF
done


Im really stuck now ... can anyone out there help me ???

Please???
Thank you ??
dk
dkbaj (1047)
151204 2003-06-09 10:17:00 > I have been asked to wirte a bash script to display
> the following as the output :

Bash is really picky about where you put spaces etc compared to other languages . If you need some examples of the syntax have a look in the system startup scripts, thats what I used to learn .


> > filestats /bin /etc /var /etc/password /usr/sbin
>
> /bin : 0 ordinary, 86 executable, 0 directories, 15 links .
> /etc : 144 ordinary, 6 executable, 65 directories, 15 links .
> /var : 0 ordinary, 0 executable, 19 directories, 1 links .
> /etc/passwd : is not a directory
> /usr/sbin : 15 ordinary, 232 executable, 0 directories, 49 links .
>
> I have gotten as far as this with the script:
>
# > clear
>
> if [ $# ieq 0 ]
>
> then
> DirList = "$PWD"elif
> DirList = "$*"
# you forgot to close the if
fi

>
>
> for
> Dir in $DirList do
# this doesn't do anything > Dir ls $Dir
>
>
# you have to use backwards quotes (shift-~) to get output from commands .
for File in `ls $Dir` do
>

# You need a space between if and [
if [ -f $File ]
> then files=$(($files + 1))
fi
> if [ -x $File ]
> then execF=$(($execF + 1))
fi
> if [ -d $File ]
> then dire=$(($dire + 1))
fi
>
> done
>
# its echo, not "echo:"
echo "numfiles:"$files
echo "numexecF":$execF
echo "numexecF":$execF
> done
>
>
> Im really stuck now . . . can anyone out there help me
> ???
>
> Please???
> Thank you ??
> dk



That should be enough to get it to run without errors . But its not complete .

A couple of other tips:
- Have a look at "declare -i" in the man page, it makes arithmetic easier .
- If you want to handle spaces in filenames correctly, you will need to have a play with the IFS variable .
- Throwing an "echo $Dir" and "echo $File" into the loops will make debugging a lot easier .
bmason (508)
151205 2003-06-09 12:38:00 Thanks alot bmason! I know about the spacing part, but the post automatically formatted it like that. ( I wanted to show everyone exactly how i wrote it but I guess that didnt work)

Yeah I havent completed it..... Another thing is.. what command do I use to get the output onto one line .... do i use either grep or cut??
Thank you ...
dk
dkbaj (1047)
151206 2003-06-09 13:09:00 > Yeah I havent completed it . . . . . Another thing is . .
> what command do I use to get the output onto one line
> . . . . do i use either grep or cut??

Just join the echo lines:

echo "numfiles: $files exec: $execF directorys: $execF"
bmason (508)
1