You are not logged in.

#1 2011-10-21 17:46:50

tasty_minerals
Member
Registered: 2011-03-30
Posts: 191

[SOLVED] Help me with "ls" shell script please

So, I was thinking about making a script similar to "ls", but a bit more fancy, just the way I want it to work.
So when I've wrote it, it works fine, but there is a bug.
It skips the filenames with spaces like "hello world". Cause I can't figure out how to separate files inside a variable, I ask if somebody may guide me here.

The script:

#!/bin/sh
#lls v1.2
#lls is a sript that works as "ls --group-directories-first" wrapped by a "file" command and some fancy colors.
#
#SCRIPT TAGS:
#DIR open -- means that user can search into this directory.
#DIR closed -- means that user can't search into this directory.
#FILE open -- means that this file is writable and readable.
#FILE closed -- means that this file is not writable and readable.
#FILE open X -- means that this file is writable, readable and executable.
#FILE closed X -- means that this file is executable only.
#FILE seen X -- means that this file is readable and executable, but not writable.
#FILE edit X -- means that this file is writable and executable, but not readable.
#FILE edit only -- means that this file is writable only.
#FILE seen only -- means that this file is readable only.
#FILE zombie -- means that this file is not writable, readable or executable.
#

file_info ( )
{
	file=$1
	file_prop=$(file -b "$file")
	if [ -d "$file" -a -x "$file" ]; then
echo -e "\033[34;5;148m${file##$PWD/}\033[39m \033[34;1;148m> DIR open\033[39m \033[30;5;148m{$file_prop}\033[39m"
tput sgr0
	elif [ -d "$file" -a ! -x "$file" ]; then
echo -e "\033[31;5;148m${file##$PWD/}\033[39m \033[31;1;148m> DIR closed\033[39m \033[30;5;148m{$file_prop}\033[39m"
tput sgr0
fi

	if [ -f "$file" -a -r "$file" -a -w "$file" -a ! -x "$file" ]; then
echo -e "${file##$PWD/} \033[32;1;148m> FILE open\033[39m \033[30;5;148m{$file_prop}\033[39m"
tput sgr0
#	elif [ -f "$file" -a ! -w "$file" -a ! -r "$file" ]; then
#echo -e "\033[31;5;148m${file##$PWD/}\033[39m \033[31;1;148m> FILE closed\033[39m \033[30;5;148m{$file_prop}\033[39m"
	elif [ -f "$file" -a ! -w "$file" -a ! -r "$file" -a -x "$file" ]; then
echo -e "\033[31;5;148m${file##$PWD/}\033[39m \033[31;1;148m> FILE closed X\033[39m \033[30;5;148m{$file_prop}\033[39m"
	elif [ -f "$file" -a -w "$file" -a -r "$file" -a -x "$file" ]; then
echo -e "\033[33;5;148m${file##$PWD/}\033[39m \033[33;1;148m> FILE open X\033[39m \033[30;5;148m{$file_prop}\033[39m"
	elif [ -f "$file" -a ! -w "$file" -a -r "$file" -a -x "$file" ]; then
echo -e "\033[31;5;148m${file##$PWD/}\033[39m \033[31;1;148m> FILE seen X\033[39m \033[30;5;148m{$file_prop}\033[39m"
	elif [ -f "$file" -a -w "$file" -a ! -r "$file" -a -x "$file" ]; then
echo -e "\033[35;5;148m${file##$PWD/}\033[39m \033[35;1;148m> FILE edit X\033[39m \033[30;5;148m{$file_prop}\033[39m"
	elif [ -f "$file" -a -w "$file" -a ! -r "$file" -a ! -x "$file" ]; then
echo -e "\033[35;5;148m${file##$PWD/}\033[39m \033[35;1;148m> FILE edit only\033[39m \033[30;5;148m{$file_prop}\033[39m"
	elif [ -f "$file" -a ! -w "$file" -a -r "$file" -a ! -x "$file" ]; then
echo -e "\033[31;5;148m${file##$PWD/}\033[39m \033[31;1;148m> FILE seen only\033[39m \033[30;5;148m{$file_prop}\033[39m"
	elif [ -f "$file" -a ! -w "$file" -a ! -r "$file" -a ! -x "$file" ]; then
echo -e "\033[30;5;148m${file##$PWD/}\033[39m \033[30;1;148m> FILE zombie\033[39m \033[30;5;148m{$file_prop}\033[39m"
tput sgr0
fi
}

curdir=$1
	if [ -z "$curdir" ]; then
curdir=$PWD
	elif [ -f $curdir ]; then
echo "Its a file!"
exit 0
fi

dirs=`ls --group-directories-first $curdir`
for files in $dirs; do
		file_info "$curdir/$files"
done

Last edited by tasty_minerals (2011-10-23 09:39:32)


lenovo thinkpad EDGE 13'

Offline

#2 2011-10-21 18:22:23

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Help me with "ls" shell script please

The last part: seems to be the culprit

dirs=`ls --group-directories-first`
for file in $dirs; do
		file_info "$curdir/$file"
done

Check

dirs=`ls --group-directories-first`
for file in $dirs; do
  echo $file
done

You shouldn't parse ls output.

Offline

#3 2011-10-22 03:37:06

steve___
Member
Registered: 2008-02-24
Posts: 452

Re: [SOLVED] Help me with "ls" shell script please

Offline

#4 2011-10-23 09:32:57

tasty_minerals
Member
Registered: 2011-03-30
Posts: 191

Re: [SOLVED] Help me with "ls" shell script please

I'm very sorry, posted a bad copy of the script, that doesn't work. Fixed


lenovo thinkpad EDGE 13'

Offline

#5 2011-10-23 09:35:11

tasty_minerals
Member
Registered: 2011-03-30
Posts: 191

Re: [SOLVED] Help me with "ls" shell script please

Ahhh, thank you guys, maybe I think about using "find" instead, its seems like a better choice.

Last edited by tasty_minerals (2011-10-23 09:35:52)


lenovo thinkpad EDGE 13'

Offline

#6 2011-10-23 09:41:02

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Help me with "ls" shell script please

tasty_minerals wrote:

I'm very sorry, posted a bad copy of the script, that doesn't work. Fixed

Your script seems to work for me - I got the colors and blinking ...

tasty_minerals wrote:

Ahhh, thank you guys, maybe I should use "find" then.

Yes, this should fix it.

Offline

#7 2011-10-23 09:43:28

tasty_minerals
Member
Registered: 2011-03-30
Posts: 191

Re: [SOLVED] Help me with "ls" shell script please

karol wrote:
tasty_minerals wrote:

I'm very sorry, posted a bad copy of the script, that doesn't work. Fixed

Your script seems to work for me - I got the colors and blinking ...

tasty_minerals wrote:

Ahhh, thank you guys, maybe I should use "find" then.

Yes, this should fix it.

blinking? ^^
no, blinking is a bug of xterm and "file" utility, try to use any other terminal, like lxterminal, or lilyterm or some vte based. I still couldn't figure out the blinking thing.
By the way "lls" does accept arguments like dirs: "lls /usr/share/". Just in case.


lenovo thinkpad EDGE 13'

Offline

#8 2011-10-23 09:45:38

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED] Help me with "ls" shell script please

I'm using rxvt-unicode .... and I'm glad it's a bug and not something you designed as a feature ;P

Offline

#9 2011-10-23 09:50:24

tasty_minerals
Member
Registered: 2011-03-30
Posts: 191

Re: [SOLVED] Help me with "ls" shell script please

lol, ok, I'm glad that you're glad


lenovo thinkpad EDGE 13'

Offline

Board footer

Powered by FluxBB