femchi
November 6, 2012, 8:30pm
1
Write a Unix shell script named 'mode' that accepts two or more arguments, a file mode, a command and an optional list of parameters and performs the given command with the optional parameters on all files with that given mode.
For example, mode 644 ls -l should perform the command ls -l on all files in the current directory that have mode 644.
I came up with this code. how can I ask the code to apply the rest of arguments in the command line to the result of the script?
#!/bin/sh
mode=$1
shift 1
ls -l |awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/) \
*2^(8-i));if(k)printf("%0o ",k);print }'|grep "$mode"
Langara College - Vancouver - Canada , Hengameh Farrahmand, CPSC 1280
(it is my first post I hope I follow all the rules )
ctsgnb
November 6, 2012, 8:43pm
2
Why to reinvent the wheel ?
man find
see the -perm
option
if it is related to translation to mode octal value you may also be interested in the following :
conv_perm(){
binary1=`echo "$1"|cut -c4,7,10|tr xstST- 011110`
binary2=`echo "$1"|cut -c2-10|tr rwsxtST- 11111000`
octal=`echo "obase=8;ibase=2;${binary1}${binary2}"|bc`
echo "$octal"
}
$ ls -l dbs* | awk '{print$1,$9}'
-rwxr-xr-x dbshut
-rwsr-s--- dbsnmp
-rwxr-xr-x dbsnmp0
-rwxr-xr-x dbsnmpj
-rw-r--r-- dbsnmpj0
-rwsr-s--- dbsnmp.sav
-rwxr-xr-x dbsnmpwd
-rwxr-xr-x dbstart
$ ls -l dbs* | while read a b b b b b b b b
> do
> printf "%4d %s\n" $(conv_perm $a) $b
> done
755 dbshut
6750 dbsnmp
755 dbsnmp0
755 dbsnmpj
644 dbsnmpj0
6750 dbsnmp.sav
755 dbsnmpwd
755 dbstart
$
femchi
November 6, 2012, 8:50pm
3
ok. the the code should be like this
#!/bin/sh
perm=$1
shift
find . -perm $perm $* {} \;
but my problem is that I dont know how I should apply the rest of the command line to the result of "find"
also let me check the code with "find" too
femchi
November 6, 2012, 9:07pm
5
#!/bin/sh
permission=$1
shift
find . -maxdepth 1 -perm $permission -exec $* {} \;
(fixed)
Probably worth checking to ensure 2 or more arguments are passed and when incorrect output message to stderr and set the exit code