Operating System - HP-UX
1825719 Members
2897 Online
109686 Solutions
New Discussion

Re: Shell Script required

 
SOLVED
Go to solution
DKC
Advisor

Shell Script required

Hello Flock,

Anybody can assist me. How we can write a shell script. I don't basic of that and i need a script.1.List all the file along with their permissions in the server.

Thanks for your prompt action.

Regards,
Dev
23 REPLIES 23
SoorajCleris
Honored Contributor

Re: Shell Script required

Hi

Why you need a script for that ??

go to /

#pwd
/

#ls -lR > /list

regards,
Sooraj
"UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity" - Dennis Ritchie
James R. Ferguson
Acclaimed Contributor
Solution

Re: Shell Script required

Hi Dev:

You could reduce (filter) the recursive output of 'ls -lR' to do:

# ls -lR /root|awk '{if (NF>3) {print $1,$NF} else {print}}'

...which might yield:


/root/.cpan:
total 26160
-rw-r--r-- FTPstats.yml
-rw-r--r-- Metadata
-rw-r--r-- histfile
drwxr-xr-x sources

/root/.cpan/sources:
total 16
drwxr-xr-x authors
drwxr-xr-x modules

/root/.cpan/sources/authors:
total 336
-rw-r--r-- 01mailrc.txt.gz

/root/.cpan/sources/modules:
total 2064
-rw-r--r-- 02packages.details.txt.gz
-rw-r--r-- 03modlist.data.gz



Redirect the output to a file as you see fit:

# ls -lR /path|awk '{if (NF>3) {print $1,$NF} else {print}}' > myls.out

Regards!

...JRF...
Steven Schweda
Honored Contributor

Re: Shell Script required

> [...]
> ...which might yield:
> [...]

It might, if all your users cooperate, and
don't create any files with funny names.

dyi # ls -lR fun
total 48
-rw-r--r-- 1 root sys 6 Apr 26 14:13 a b c
-rw-r--r-- 1 root sys 6 Apr 26 14:13 d e f
-rw-r--r-- 1 root sys 2 Apr 26 14:15 x

dyi # ls -lR fun | awk '{if (NF>3) {print $1,$NF} else {print}}'
total 48
-rw-r--r-- c
-rw-r--r-- f
-rw-r--r-- x

Everything's complicated, I always say.

"find" tends to have a good idea of
what/where a file name really is. The output
format of "ls -l" is such a mess that trying
to parse it (in any elementary way) is asking
for trouble. (And sometimes you'll get what
you ask for.)
S.N.S
Valued Contributor

Re: Shell Script required

As Cleris asked,

Why Dev, do you need a full listing?
Did anyone do a chown -R from / directory?

Let us know - then we can think of the best fitting solution.

SNS




"Genius is 1% inspiration, 99% Perspiration" - Edison
James R. Ferguson
Acclaimed Contributor

Re: Shell Script required

Hi (again):

> Steven: It might, if all your users cooperate, and don't create any files with funny names.

You are correct of course. I was offering a simplistic solution under the conditions that "reasonable" file naming had been followed. Yes, we both know that many users (intentionally or otherwise) are not reasonable or knowledgeable of corner cases.

If I wanted more rigor, I'd do:

# perl -MFile::Find -we 'find(sub{printf "%04o %-s\n",(stat(_))[2]&07777,$File::Find::name if -f $_},".")'

...or, if I'm running Linux:

# find . -type f -printf "%04m %h/%f\n"

Regards!

...JRF...
Raj D.
Honored Contributor

Re: Shell Script required

To Print: File_perm and #File_names:

# find / -type f -exec ls -l {} \; |awk '{for (i=2;i<9;i++){$i=""};print $0}'

" If u think u can , If u think u cannot , - You are always Right . "
DKC
Advisor

Re: Shell Script required

Hi ,

Thanks to all of you for provinding Valuable infomation.

As somebody ask why you need this script
>> As Business requirement. Scripting is manadatory thing, so i want learn also.

Could you please assist me or can you share some document regarding shell scripting I want learn and create some scripting(HP-UX).

I would be thankfull to all if you will be provide some document regarding same.

Regards,
Dev
DKC
Advisor

Re: Shell Script required

Hi Sooraj,

# ls -LR >/list gives only the listing of file. As i mention in questions I need all files with permission.

Regards,
Dev
SoorajCleris
Honored Contributor

Re: Shell Script required

Hi Devkant,

Listening is also important like reading!!!

I have mentioned -lR ( lowercase l-el and capital R)...

Unix is a case sensitive OS! different switches -small mistakes - bigger differences !!!

:)

Regards,
Sooraj
"UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity" - Dennis Ritchie
SoorajCleris
Honored Contributor

Re: Shell Script required

http://forums11.itrc.hp.com/service/forums/helptips.do?#33
"UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity" - Dennis Ritchie
DKC
Advisor

Re: Shell Script required

Hi Sooraj,

# ls -lR > /list
#
I got O/P only when i have given ls -lR

but its O/P is too big.

Thanks & Regards,
Dev
Dennis Handly
Acclaimed Contributor

Re: Shell Script required

>I got O/P only when I have given ls -lR but its O/P is too big.

What do you want instead?
DKC
Advisor

Re: Shell Script required

Hi Dennis,

As i am learning scripting and expect that after creating scripting o/p will comes as below requirement.

1.List of the files with permissions 755.
2. List all the files created or modified 2 days back.
3.List all the files owned by certain user

Thanks for assisting me.

Regards,
Dev
James R. Ferguson
Acclaimed Contributor

Re: Shell Script required

Hi (again) Dev:

> 1.List of the files with permissions 755.

# find /path -type f -perm 755

> 2. List all the files created or modified 2 days back.

# find /path -type f -mtime +1

...which yields files older than 48-hours.

> 3.List all the files owned by certain user

# find /patch -type f -user dev

See the manpages for 'find()'.

Regards!

...JRF...
DKC
Advisor

Re: Shell Script required

Hi JRF,

Thanks a lot for your quick response.

Now i can see the O/P but if you have script for that than really its worthfull for me because i am learning scripting.

Scripting is always better than command.

Hope you will share scripting of that if you have.

Reagrds,
Dev
S.N.S
Valued Contributor

Re: Shell Script required

Hi Dev,

Scripting doesn't always need to be:
sh scriptname.sh

JRF's "commands" you have seen, are very much the basis of the complex scripts that are to be written later.

Now, the question here is - did those tools made it easier that the normals commands ls ....Well, you got the answer :-)

This is the start, the base.
Hard works pays off, eventually - Happy Learning!

HTH
SNS
"Genius is 1% inspiration, 99% Perspiration" - Edison
Raj D.
Honored Contributor

Re: Shell Script required

Devkant,

Hope all above solutions answers your question.

However to learn shell scripting you have to spend some time dedicatedly:

- get the book/documentation.
- get the system and unix command prmpt ready.
- read theory.
- write small script and practice.
- for any doubt question 1. google it or , ask for help in the forum with examples.
- If you are very new , it is suggested to spend 4 to 8 weeks learning the prgramming and flow control, looping , condition , string operation ,regular expressions etc. And you will be good soon..




Finally check out few documents here:

#--------------------------------------
1. Unix Shell scripting Tutorial:
http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html

2. The Bourne Shell:
http://www.grymoire.com/Unix/Sh.html


3. Linux Shell Scripting Tutorial v1.05r3 ( A Beginner's handbook ):
http://www.freeos.com/guides/lsst/

#-----------------------------------------


Good Luck and Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: Shell Script required

Devkant,

Also check this out:
UNIX shell scripting with ksh/bash::
####################################
The goals of this class are to enable you to:
Learn what kinds of problems are suited to shell scripts
Review the most commonly used Unix commands that are useful in shell scripts.
Write simple shell scripts using the Bourne, Korn or Bash shells
-------------------

very good ,and with examples:
Started with basic script like:

1: #!/bin/sh
2: date
3: pwd
4: du -k
################################


Link:
http://www.dartmouth.edu/~rc/classes/ksh/print_pages.shtml


Good Luck,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
SoorajCleris
Honored Contributor

Re: Shell Script required

Hi Dev,

===> Scripting is always better than command.


I wonder how you can have script with out command!!!!!

To meet your requirement to find out all those specific files , only one single line command is required.



Regards,
Sooraj
"UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity" - Dennis Ritchie
DKC
Advisor

Re: Shell Script required

Hi,

Thanks a lot for guiding me to learn shell scripting. I will follow the same way as you mention in forum.

Soorj- 'Scripting is set of command ' In limited time you will find many o/P with the help of scripting in that way i was mention scripting is better than command.

Regards,
Dev
Suraj K Sankari
Honored Contributor

Re: Shell Script required

Hi Devkant,

>>Now i can see the O/P but if you have script for that than really its worthfull for me because i am learning scripting.
>>Scripting is always better than command.
>>Hope you will share scripting of that if you have.

Just copy/paste into a file this 3 lines, save the file and execute at the prompt

vi myscript
find /path -type f -perm 755
find /path -type f -mtime +1
find /patch -type f -user dev
save the file

execute the file
$sh ./myscript

set of commands kept into a file and execute thats called scripting.

Suraj

S.N.S
Valued Contributor

Re: Shell Script required

Hi Dev,

Hope that we have been able to help you out!
Let Us know your progress when U get stuck

Seems like Cleris & Dev are pals
- lot of "quoted" interactions amongst them

Please close the thread if you are satisfied..

Danke/Merci
SNS
"Genius is 1% inspiration, 99% Perspiration" - Edison
DKC
Advisor

Re: Shell Script required

Hi,

Thanks to all.

I got Satisfactory answer.

Regards,
Dev