Goal: To highlight the entire line of my following output that has a directory without execute permissions for other.
How can I detect in my "other" column if the 3rd permission bit is either a - or T/t and based on that, highlight entire line? I know the highlight command, it's tput rmso I think, that's not the problem. It's the detection and manipulation that is.
Hey, so I'm working on a script that will list directories in long format, then format that output. Here is the output of my program:
Owner Group Other Filename
----- ----- ----- --------
d r w x r - x r - x /
d r w x r - x r - x home
d r w x r - x r - x eveo
d r w x r - x r - x Desktop
d r w x r - x r - x scripts
Everything works as expected so far except for one thing. Here is my code.
if [ ! -d $1 ]
then
echo dirpath: $1 is not a valid directory name >&2
exit 1
elif [ $# = 2 ]
then
echo Usage: dirpath [ dir-name ] >&2
exit 1
elif [ $# = 0 ]
then
echo Usage: dirpath [ dir-name ] >&2
exit 1
elif [ $# = 1 ]
then
echo " Owner Group Other Filename"
echo " ----- ----- ----- --------"
echo $PWD | sed 's/\//\n/g' > file1
sed '1s/?*/\//' file1 > file2
permissions1=$(ls -ld | cut -c1-4 | sed 's/./& /g')
permissions2=$(ls -ld | cut -c5-7 | sed 's/./& /g')
permissions3=$(ls -ld | cut -c8-10 | sed 's/./& /g')
while read line
do
echo -e "$permissions1 $permissions2 $permissions3 $line"
done < file2
exit 0
else
echo Usage: dirpath [ dir-name ] >&2
exit 1
fi
EDIT: FINISHED IT MYSELF :D
UPDATED SOURCE:
if [ ! -d $1 ]
then
echo dirpath: $1 is not a valid directory name >&2
exit 1
elif [ $# = 2 ]
then
echo Usage: dirpath开发者_如何学运维 [ dir-name ] >&2
exit 1
elif [ $# = 0 ]
then
echo Usage: dirpath [ dir-name ] >&2
exit 1
elif [ $# = 1 ]
then
echo " Owner Group Other Filename"
echo " ----- ----- ----- --------"
normaldir="$PWD"
cd $1
targetdir="$PWD"
cd $normaldir
temp1=/tmp/$$temp1
temp2=/tmp/$$temp2
echo "$targetdir" | sed 's/\//\n/g' > $temp1
sed '1s/?*/\//' $temp1 > $temp2
pathname=
while read line
do
if [ "$pathname" = "/" ]
then
pathname="$pathname$line"
else
pathname="$pathname/$line"
fi
permissions1=$(ls -ld "$pathname" | cut -c1-4 | sed 's/./& /g')
permissions2=$(ls -ld "$pathname" | cut -c5-7 | sed 's/./& /g')
permissions3=$(ls -ld "$pathname" | cut -c8-10 | sed 's/./& /g')
otherexec=$(ls -ld "$pathname" | cut -c10 )
if [ "$otherexec" = "-" ] || [ "$otherexec" = "T" ]
then
tput smso
fi
############################################################
echo -e "$permissions1 $permissions2 $permissions3 $line"
############################################################
if [ "$otherexec" = "-" ] || [ "$otherexec" = "T" ]
then
tput rmso
fi
done < $temp2
#rm $tempfile1 file2
exit 0
else
echo Usage: dirpath [ dir-name ] >&2
exit 1
fi
An easier way to check execute permission in Bash is
for file in `find $pathname -perm /111` ; do
echo "$file is executable/n"
done
find is incredibly useful, the -print0 option terminates each line with a null character instead of a newline.
精彩评论