Aakash Kushwaha
Aakash Kushwaha

Reputation: 45

using tr command to read permission

Following commands have been executed by me

[root@gblabvl31 Aakash]# cat > file1.txt
-rwx
[root@gblabvl31 Aakash]# cat file1.txt |tr '[-rwx]' '[0421]' > file2.txt
[root@gblabvl31 Aakash]# cat file2.txt
-]]]

I could not understand why the expected output which is

0421

is not achieved

Upvotes: 1

Views: 240

Answers (2)

Adrian Frühwirth
Adrian Frühwirth

Reputation: 45626

tr is a little special and expects its parameters to already be character classes, thus

$ echo '-rwx' | tr -- '-rwx' '0421'
0421

yields the expected result.

Upvotes: 2

heemayl
heemayl

Reputation: 42087

Get rid of the character class:

$ tr -- '-rwx' '0421' <<<'-rwx'
0421

Upvotes: 1

Related Questions