LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-07-2012, 11:02 AM   #1
maxwel6064
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Rep: Reputation: Disabled
Bash script


Hello. I need your help in bash scripting. could you help me, how to show all files, which are having permission "read only", using only if, for or while? I'm waiting for your answer.
 
Old 11-07-2012, 11:08 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
there are probably more elegant ways but maybe you can parse the info from ls -a -l.

what have you tried so far. we are allowed to assist you with your homework but we are not allowed to do it for you.
 
Old 11-07-2012, 11:18 AM   #3
maxwel6064
LQ Newbie
 
Registered: Nov 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Well, i should do this script:
to show all files in directory (should enter from keyboard), which are having permission "read only". i tried to do it, using this script:

#/bin/bash
echo "enter name of directory"
read a
for i in $a
do
if [ -r $a ]
then
echo "$i"
fi
done
I think, that script should look like similar to my code, but unfortunately my code isn't working and don't show a result... could you edit my code? I'm waiting for your advices
 
Old 11-07-2012, 11:26 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,027

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
^ use code tags to preserve formatting. for me your code prints the name of the directory i entered.

edit: after the the user enters in a directory name, try running find to get a list of the files in that directory.

Last edited by schneidz; 11-07-2012 at 11:31 AM.
 
Old 11-07-2012, 04:15 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
for i in $a
Since $a only contains a single value, the loop will only cycle once. At least probably once. Since you didn't quote it, the shell would break up any multi-word entries and attempt to cycle over the parts.

What you really want is probably this:

Code:
for i in "$a"/*
Also, The -r test isn't for "read-only", it's for "readable", which is a different thing. What you should probably do is use stat to get the full set of file permissions and then test against that. Parsing ls for file info is not recommended.

How can I get a file's permissions (or other metadata) without parsing ls -l output?
http://mywiki.wooledge.org/BashFAQ/087

You should also add a test to ensure that $a is a valid directory before running the loop on it. I also suggest using more meaningful variable names. Single letters tend to make code less understandable and so should be avoided in all but the simplest cases.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 10:46 AM
[SOLVED] Run multiple bash and php scripts from a bash script charu Programming 5 07-26-2011 03:40 AM
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM


All times are GMT -5. The time now is 06:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration