এখানে প্রশ্নের উত্তর এর ওয়াই (উপেক্ষা প্রশ্ন এক্স ), অপ এর প্রচেষ্টা দ্বারা অনুপ্রাণিত:
#!/bin/bash
LC_COLLATE=C
while read ls_out
do
extra=0
perms=0
for i in {1..9}
do
# Shift $perms to the left one bit, so we can always just add the LSB.
let $((perms*=2))
this_char=${ls_out:i:1}
# If it's different from its upper case equivalent,
# it's a lower case letter, so the bit is set.
# Unless it's "l" (lower case L), which is special.
if [ "$this_char" != "${this_char^}" ] && [ "$this_char" != "l" ]
then
let $((perms++))
fi
# If it's not "r", "w", "x", or "-", it indicates that
# one of the high-order (S/s=4000, S/s/L/l=2000, or T/t=1000) bits
# is set.
case "$this_char" in
([^rwx-])
let $((extra += 2 ** (3-i/3) ))
esac
done
printf "%o%.3o\n" "$extra" "$perms"
done
উপরের কয়েকটি বাশিজম রয়েছে। নিম্নলিখিত সংস্করণটি POSIX- অনুগত বলে মনে হচ্ছে:
#!/bin/sh
LC_COLLATE=C
while read ls_out
do
extra=0
perms=0
for i in $(seq 1 9)
do
# Shift $perms to the left one bit, so we can always just add the LSB.
: $((perms*=2))
this_char=$(expr "$ls_out" : ".\{$i\}\(.\)")
# Lower case letters other than "l" indicate that permission bits are set.
# If it's not "r", "w", "x", or "-", it indicates that
case "$this_char" in
(l)
;;
([a-z])
: $((perms+=1))
esac
# If it's not "r", "w", "x", or "-", it indicates that
# one of the high-order (S/s=4000, S/s/L/l=2000, or T/t=1000) bits
# is set.
case "$this_char" in
([!rwx-])
: $((extra += 1 << (3-i/3) ))
esac
done
printf "%o%.3o\n" "$extra" "$perms"
done
মন্তব্য:
ব্যবহার:
$ echo drwxr-xr-x | chmod-format
0755
$ echo -rwsr-sr-x | chmod-format
6755
$ echo -rwSr-Sr-- | chmod-format
6644
$ echo -rw-r-lr-- | chmod-format
2644
$ echo ---------- | chmod-format
0000
এবং, হ্যাঁ, আমি জানি echoযে পাঠ্যটি শুরু হতে পারে তা ব্যবহার না করাই ভাল -; আমি প্রশ্ন থেকে ব্যবহারের উদাহরণটি অনুলিপি করতে চেয়েছিলাম। দ্রষ্টব্য, স্পষ্টতই, এটি 0 তম অক্ষর (যেমন, নেতৃস্থানীয় d/ b/ c/ -/ l/ p/ s/ / D) এবং 10 তম ( +/ ./ @) উপেক্ষা করে । এটি ধরে নেওয়া হয় যে রক্ষণাবেক্ষণকারীরা তৃতীয়, ষষ্ঠ, বা নবম অবস্থানে lsকখনই r/ Rবা w/ Wবৈধ অক্ষর হিসাবে সংজ্ঞায়িত করবেন না
(এবং যদি তারা করেন তবে তাদের লাঠি দিয়ে প্রহার করা উচিত )।
এছাড়াও, আমি সবেমাত্র নীচের কোডটি পেয়েছি, ক্যাস দ্বারা ,
কীভাবে / var এর অধীনে সমস্ত ফাইলের ডিফল্ট গ্রুপ / ব্যবহারকারীর মালিকানা পুনরুদ্ধার করতে হবে :
let perms=0
[[ "${string}" = ?r???????? ]] && perms=$(( perms + 400 ))
[[ "${string}" = ??w??????? ]] && perms=$(( perms + 200 ))
[[ "${string}" = ???x?????? ]] && perms=$(( perms + 100 ))
[[ "${string}" = ???s?????? ]] && perms=$(( perms + 4100 ))
[[ "${string}" = ???S?????? ]] && perms=$(( perms + 4000 ))
[[ "${string}" = ????r????? ]] && perms=$(( perms + 40 ))
[[ "${string}" = ?????w???? ]] && perms=$(( perms + 20 ))
[[ "${string}" = ??????x??? ]] && perms=$(( perms + 10 ))
[[ "${string}" = ??????s??? ]] && perms=$(( perms + 2010 ))
[[ "${string}" = ??????S??? ]] && perms=$(( perms + 2000 ))
[[ "${string}" = ???????r?? ]] && perms=$(( perms + 4 ))
[[ "${string}" = ????????w? ]] && perms=$(( perms + 2 ))
[[ "${string}" = ?????????x ]] && perms=$(( perms + 1 ))
[[ "${string}" = ?????????t ]] && perms=$(( perms + 1001 ))
[[ "${string}" = ?????????T ]] && perms=$(( perms + 1000 ))
আমি এই কোডটি পরীক্ষা করেছি (তবে পুরোপুরি নয়) এবং এটি কাজ করে বলে মনে হচ্ছে, এটি স্বীকৃতি দেয় না lবা Lষষ্ঠ স্থানে রয়েছে। নোট করুন, যদিও, যদিও উত্তরটি সরলতা এবং স্বচ্ছতার ক্ষেত্রে উচ্চতর, তবুও খনিটি আসলে খাটো ( লুপের ভিতরে কেবল কোড গণনা করা ; কোড যা একটি একক -rwxrwxrwxস্ট্রিং পরিচালনা করে , মন্তব্যগুলি গণনা করে না), এবং এটি আরও ছোট করা যেতে পারে প্রতিস্থাপন
সঙ্গে ।if condition; then …condition && …
অবশ্যই, আপনার আউটপুট পার্স করা উচিত নয়ls ।
stat। তোমার কি আছে? (এটি একটি জিএনইউ সরঞ্জাম, তাই ইউনিক্সে নয়, লিনাক্সে বেশিরভাগ ক্ষেত্রে উপলভ্য))