You are not logged in.
Hi community,
I've got as homework to write a script which uses the command file to check if a file is really a text file, and if yes echo its name.
I wrote the following
#!/bin/bash
for filetype in *
do
a= file $filetype | cut -f2 -d' '
b="ASCII"
if [ "$a"="$b" ]
then
echo "$filetype"
fi
done
however, this does not work.
It just echoes the filetype and all the filenames.
directory
Desktop
Bourne-Again
existe
gzip
kernel26-one-2.6.27-3-i686.pkg.tar.gz
Bourne-Again
list
Bourne-Again
pidof
empty
ps
Bourne-Again
realjpg
Bourne-Again
taille
ASCII
temp.txt
directory
themes
Any ideas?
Why is the filetype echoed, when I declare it as a variable?
Why does the condition thing not work?
Basically, what am I missing?
Offline
Change:
a= file $filetype | cut -f2 -d' '
to:
a=$(file $filetype | cut -f2 -d' ')
Offline
Don't put a space after 'a='. What you're doing there is just executing file and not capturing it's output (that's why you see it on the screen). You need to capture the output and store it in $a
< Daenyth> and he works prolifically
4 8 15 16 23 42
Online
You had the right idea
a= file $filetype | cut -f2 -d' '
1. when assigning do not put spaces around =
2. you are collecting the output of the command, so use $( ), or backtick
if [ "$a"="$b" ]
when comparing in [ put spaces around =
Then it will work as expected.
Offline
Googling for TLDP's bash scripting guide should be helpful.
[git] | [AURpkgs] | [arch-games]
Offline
thanks for the ressource.
here's the link
http://tldp.org/LDP/abs/html/
Thanks also for the answers, they were really helpful.
ok, I wanted to post another problem I had but while I was pasting my code in here I realised my mistake.
Offline