Reputation: 1772
I'm writing a script that takes a parameter and reads the output of ls -l
. It then displays the user and the name of the file STARTING with that parameter.
For example :
$> ls -l | ./script.sh "o"
John ok_test
RandomUser o_file
My script works just fine, but I have one (minor) problem that I truly need to fix : it does not align the file names as in the example above.
Here is the said script :
#!/bin/bash
while read hello
do
name=$(echo $hello | cut -d' ' -f9 | grep "^"$1)
if [ $? = 0 ]
then
log=$(echo $hello | cut -d' ' -f3)
echo -n -e $log'\t' && echo $name
fi
done
Does anyone of you know a way to align the output no matter the size of the user name?
Thanks a lot.
Upvotes: 0
Views: 137
Reputation: 246774
I'd rewrite that as:
#!/bin/bash
while read -ra hello; do
name=${hello[8]}
if [[ $name == "$1"* ]]; then
log=${hello[2]}
echo "$log $name"
fi
done | column -t
read -ra
splits the input line and stores the words in the "hello" array.
[[ $name == "$1"* ]]
is a built-in way to check if a string begins with some value.
Upvotes: 1