473,320 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Output of ls command in octal

Some time ago I copied a bash shell script from a site which I cannot trace. It was written in bash and awk and was not good but superb ! It was about 25 lines long. I had made it into a function and was using it regularly but in a change of distro I lost it . Can anyone help?
Sep 20 '10 #1

✓ answered by pwilliams

Expand|Select|Wrap|Line Numbers
  1.  
  2. function lso {
  3. ls -l | awk '{
  4.     k = 0
  5.     s = 0
  6.     for( i = 0; i <= 8; i++ )
  7.     {
  8.         k += ( ( substr( $1, i+2, 1 ) ~ /[rwxst]/ ) * 2 ^( 8 - i ) )
  9.     }
  10.     j = 4 
  11.     for( i = 4; i <= 10; i += 3 )
  12.     {
  13.         s += ( ( substr( $1, i, 1 ) ~ /[stST]/ ) * j )
  14.         j/=2
  15.     }
  16.     if ( k )
  17.     {
  18.         printf( "%0o%0o ", s, k )
  19.     }
  20.     print
  21. }'
  22. }
  23.  
  24. function lsa {
  25. ls -al | awk '{
  26.     k = 0
  27.     s = 0
  28.     for( i = 0; i <= 8; i++ )
  29.     {
  30.         k += ( ( substr( $1, i+2, 1 ) ~ /[rwxst]/ ) * 2 ^( 8 - i ) )
  31.     }
  32.     j = 4 
  33.     for( i = 4; i <= 10; i += 3 )
  34.     {
  35.         s += ( ( substr( $1, i, 1 ) ~ /[stST]/ ) * j )
  36.         j/=2
  37.     }
  38.     if ( k )
  39.     {
  40.         printf( "%0o%0o ", s, k )
  41.     }
  42.     print
  43. }'
  44. }
  45.  
Similar but different.
ashitpro,IRYHS !

5 2667
Oralloy
985 Expert 512MB
@pwilliams,

Can you give us an example of what the script did?

Your description isn't very clear. We know something about processing ls output into octal. However a concrete request might help.
Sep 20 '10 #2
ashitpro
542 Expert 512MB
I believe you need output permissions in octal format ?

for example if original output is:
Expand|Select|Wrap|Line Numbers
  1. -rw-r--r-- 1 root root    24405 Sep 20 16:56 a.txt
you need it like this:
Expand|Select|Wrap|Line Numbers
  1. 644 -rw-r--r-- 1 root root    24405 Sep 20 16:56 a.txt
If this is what you wanted then you can use;
Expand|Select|Wrap|Line Numbers
  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}'
Sep 21 '10 #3
Oralloy ,ashitpro many thanks for your replies.
With a red face I must confess,the file came to light in a totally wrong directory , on a usb stick I had forgotten about.Would anybody be interested in seeing it?
Sep 21 '10 #4
ashitpro
542 Expert 512MB
send the function you are talking about.
Sep 22 '10 #5
Expand|Select|Wrap|Line Numbers
  1.  
  2. function lso {
  3. ls -l | awk '{
  4.     k = 0
  5.     s = 0
  6.     for( i = 0; i <= 8; i++ )
  7.     {
  8.         k += ( ( substr( $1, i+2, 1 ) ~ /[rwxst]/ ) * 2 ^( 8 - i ) )
  9.     }
  10.     j = 4 
  11.     for( i = 4; i <= 10; i += 3 )
  12.     {
  13.         s += ( ( substr( $1, i, 1 ) ~ /[stST]/ ) * j )
  14.         j/=2
  15.     }
  16.     if ( k )
  17.     {
  18.         printf( "%0o%0o ", s, k )
  19.     }
  20.     print
  21. }'
  22. }
  23.  
  24. function lsa {
  25. ls -al | awk '{
  26.     k = 0
  27.     s = 0
  28.     for( i = 0; i <= 8; i++ )
  29.     {
  30.         k += ( ( substr( $1, i+2, 1 ) ~ /[rwxst]/ ) * 2 ^( 8 - i ) )
  31.     }
  32.     j = 4 
  33.     for( i = 4; i <= 10; i += 3 )
  34.     {
  35.         s += ( ( substr( $1, i, 1 ) ~ /[stST]/ ) * j )
  36.         j/=2
  37.     }
  38.     if ( k )
  39.     {
  40.         printf( "%0o%0o ", s, k )
  41.     }
  42.     print
  43. }'
  44. }
  45.  
Similar but different.
ashitpro,IRYHS !
Sep 22 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Sean McIlroy | last post by:
I recently found out that unicode("\347", "iso-8859-1") is the lowercase c-with-cedilla, so I set out to round up the unicode numbers of the extra characters you need for French, and I found them...
5
by: KB | last post by:
Hi, This may be a rudimentary question: How to convert a string like '777' to an octal integer like 0777, so that it can be used in os.chmod('myfile',0777)? I know the leading zero is...
8
by: bearophileHUGS | last post by:
Hello, I have four things to ask or to suggest, sorry if they seem basic or already discussed. ------------------- I am still ignorant about Tkinter. This little program, after pressing the...
1
by: Jim | last post by:
I need to input a stream of data that is a string. In that string are some octal numbers. What happen, did you forget the ushort.Parse(string,...) that accepts octal numbers? You have decimal...
27
by: fuch6921 | last post by:
I want to read in an Octal number argument and have it stored as an octal number. For instance the user will type: ./a.out 777 and it will store the octal number 777. But it atoi does this as an...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
7
by: Gary Brown | last post by:
Hi, I have a whole bunch of integer constants that are best given in octal (PDP-1 opcodes). It makes a huge difference in readability and authenticity if these can be entered in octal. I...
10
by: laurent.pauloin | last post by:
Hello, I would like to print a signed octal. With a signed short it's printf %hi but what about an octal ? Tk !
1
by: HaifaCarina | last post by:
Here's the code is used but...but still something is wrong... i need help... /*CONVERTING DECIMAL TO OCTAL*/ String inputDeci,octal=""; int deci,count=0, i, h; ...
25
by: notahipee | last post by:
I have been trying to cin an number from 0 to 9 with a leading 0. For example 00 or 07. I was using a switch case. switch (int) { case 01: break; case 02: break;..... My problem arises at 08...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.