/** * vBulletin 3.8.9 Beta 4 CSS * Style: 'LQ Style - child of default'; Style ID: 7 */ @import url("https://www.linuxquestions.org/questions/clientscript/vbulletin_css/style-06c5b077-00007.css"); Help With Homework Assignment
LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
 
Search this Thread
Old 03-02-2014, 02:11 PM   #1
boygenuis
Member
 
Registered: Oct 2007
Location: The Fire Swamp
Distribution: SUSE 12.3, Fuduntu, Fedora 18, Ubuntu 12.04
Posts: 38

Rep: Reputation: 26
Help With Homework Assignment


I'm having some issue with a homework assignment on shell scripting. The instructor gave me this pseudo code:

Code:
BEGIN
    set FINDALL to false
    IF $1 equals -a THEN
        set FINDALL to TRUE
        dispose of $1
    END-IF
    FOR each Positional Parameter ($file) DO
        IF $file is a pathname THEN
            IF if $file is executable and not a directory THEN
                echo $file
            ELSE
                echo $file  is NOT Found
            END-IF
        ELSE
            set FOUND to false
            # Don't forget to handle the special cases of PATH
            FOR  P in PATH (: separated list) DO
                IF $P/$file is executable and not a directory THEN
                    set FOUND to true
                    echo $P/$file
                    IF FINDALL is FALSE THEN
                        BREAK FROMTHE for  LOOP
                    END-IF
                ELSE
                END-IF
                IF FOUND is FALSE THEN
                    echo $file is NOT FOUND
                END-IF
            END-FOR
        END-IF
    END-FOR
END
I can't quite get my code to work. Like, I'll run it and get no echo. I also fear I am not commenting well or formatting well for general readability. If someone has some time and wants to relive their college freshmen days, can you point me in a good direction? Thank you.

Code:
#! /bin/sh 

#set a flag to check if you should search all for all occurances
#by default, you will only find the first occurance 

FINDALL=FALSE
	case $1 in
	   -a)
	     FINDALL=TRUE
		#throw away this argument
	     shift
	     ;;
	esac
for file
	do
	#if $file is a pathname
	case $file in
	*/*)
	#if $file is executable and not a directory	
		if [ -x "$file" ]
			then
			if [ !-d "$file" ]
				then
				echo "$file"
		else
			echo "$file NOT FOUND" 
			fi
		fi
		;;
	*)	#no / in the filename
		FOUND=FALSE
		;;
	esac
#handle special cases of PATH
for P in `echo $PATH | sed -e 's/^:/.:/' -e 's/::/:.:/' -e 's/:$/:./' -e 's/:/ /g'`
	do			#start the do loop through PATH	
	#test if $P/$file is a directory or executable
	case $file in
	*)
		if [ -f "$P/$file" ]
			then
			if [ !-d "$P/$file" ]
				then
				FOUND=TRUE
				echo "$P/$file"
			else [ FINDALL=FALSE ]
			break 
			fi
		fi
		;;
	esac
	done
	if [$FOUND=FALSE]
		then
		echo "$file NOT FOUND" 
		fi
done

Last edited by boygenuis; 03-07-2014 at 12:20 PM. Reason: incorrect code added
 
Old 03-02-2014, 03:46 PM   #2
notKlaatu
Member
 
Registered: Sep 2010
Location: Wellington, New Zealand
Distribution: Slackware
Posts: 468

Rep: Reputation: 127Reputation: 127
I'm assuming the purpose of this script is to find whether or not the file issued as an argument actually exists or not..?

You're close, I think.

As a general rule, it's a good idea to "protect" your variables. So instead of writing things like

Code:
$P/$file
it is considered safer to write it:
Code:
${P}/${file}
And finally, I would say it's a good idea to insert "echo" statements A LOT when initially testing stuff. For me, these act a little like debugging statements, and it helps me know where exactly what is going on as the programme runs.

For instance, before entering an 'if' statement, precede it with a simple
Code:
echo "DEBUG: an if statement to test if file is exec or not"
or something like that. This way, you see what tests have passed and failed, and that ought to help you debug your own logic.

Last edited by notKlaatu; 03-03-2014 at 12:12 AM. Reason: accidentally hit submit in the middle of writing..
 
Old 03-02-2014, 04:31 PM   #3
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,857

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450
Within the test command, the '=' and '==' are equivalent for bash. In fact, '=' is more compatible with sh.

You should certainly analyze the errors generated by the script. Also try using the 'bash -x' option to enter debug mode.
Also see:
http://www.cyberciti.biz/tips/debugg...ll-script.html

Last edited by metaschima; 03-02-2014 at 04:34 PM.
 
1 members found this post helpful.
Old 03-02-2014, 07:45 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,899

Rep: Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096
As not mentioned earlier, please place code / data in [code][/code] tags so it is more readable and maintains your formatting.
 
Old 03-03-2014, 12:14 AM   #5
notKlaatu
Member
 
Registered: Sep 2010
Location: Wellington, New Zealand
Distribution: Slackware
Posts: 468

Rep: Reputation: 127Reputation: 127
Quote:
Originally Posted by metaschima View Post
Within the test command, the '=' and '==' are equivalent for bash. In fact, '=' is more compatible with sh.

You should certainly analyze the errors generated by the script. Also try using the 'bash -x' option to enter debug mode.
Also see:
http://www.cyberciti.biz/tips/debugg...ll-script.html
I've edited my post just so it does not contain incorrect information for others who read this.

Thanks for the info, metaschima.
 
Old 03-03-2014, 07:02 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
There are two unmatched fis and $file never has a value assigned to it.

Here's the script with indentation
Code:
#! /bin/sh

#set a flag to check if you should search all for all occurances
#by default, you will only find the first occurance

FINDALL=FALSE
case $1 in
    -a)
        FINDALL=TRUE
        #throw away this argument
        shift
    ;;
esac
for file
do
    #if $file is a pathname
    case $file in
        */*)
            #if $file is executable and not a directory
            if [ -x "$file" ]
            then
                if [ !-d "$file" ]
                then
                echo "$file"
            else
                echo "$file NOT FOUND"
            fi
            fi    # <<  Unmatched fi
            ;;
        *) #no / in the filename
            FOUND=FALSE
            ;;
    esac
    #handle special cases of PATH
    for P in `echo $PATH | sed -e 's/^:/.:/' -e 's/::/:.:/' -e 's/:$/:./' -e 's/:/ /g'`
    do #start the do loop through PATH
        #test if $P/$file is a directory or executable
        case $file in
            *)
                if [ -f "$P/$file" ]
                then
                    if [ !-d "$P/$file" ]
                    then
                    FOUND=TRUE
                    echo "$P/$file"
                else [ FINDALL=FALSE ]
                    break
                fi
                fi    # <<  Unmatched fi
                ;;
        esac
    done
    if [$FOUND=FALSE]
    then
        echo "$file NOT FOUND"
    fi
done
 
1 members found this post helpful.
Old 03-03-2014, 02:24 PM   #7
boygenuis
Member
 
Registered: Oct 2007
Location: The Fire Swamp
Distribution: SUSE 12.3, Fuduntu, Fedora 18, Ubuntu 12.04
Posts: 38

Original Poster
Rep: Reputation: 26
Thank you, all, for your help. I've tried to make my edits based on your recommendations and here's what I've got so far. Also, because I forgot to put it earlier, the objective for this program is, "Write a shell script to locate executable files. This script takes a list of file names from the command line and determines which would be executed had these names been given as commands."

Code:
#! /bin/sh -xv

#Student: boygenuis
#Course: Shell Scripting
#Assignment: Locating Executable Files
#Date: 03 MAR 2014

#set a flag to check if you should search all for
#all occurrences. By default, you will only find 
#the first occurrence

file="$1"
FOUND="$2"
 
$FINDALL = FALSE
	case $1 in
        -a)
		FINDALL = TRUE
		shift  #throw away this argument
		;;
	esac
for file
	do
	#if $file is a pathname
	case $file in
        */*)
        #if $file is executable and not a
	#directory
		if [ -x "$file" -a ! -d "$file" ]
			then
			echo "$file"
		else
			echo "$file NOT FOUND"
		fi
		;;
#handle the special cases of PATH, start the DO loop
#through PATH, and test if $P/$file is a directory or
#executable
	*) 
		for P in `echo $PATH | sed -e 's/^:/.:/' -e 's/::/:.:/' -e 's/:$/:./' -e 's/:/ /g'`
                do 
                case $file in
                *)
			if [ -f "$P/$file" -a ! -d "$P/$file" ]
                        	then
				FOUND = TRUE
                                        echo "$P/$file"
			echo "DEBUG: Test if if [ FINDALL = FALSE ]"
			if [ $FINDALL = FALSE ]
				then
				break
				fi
			fi
			;;
                esac
                done
                if [ $FOUND = FALSE ]
			then
			echo "$file NOT FOUND"
		fi
		;;
	esac
done

Last edited by boygenuis; 03-07-2014 at 12:21 PM. Reason: forgot code tags
 
1 members found this post helpful.
Old 03-03-2014, 04:13 PM   #8
notKlaatu
Member
 
Registered: Sep 2010
Location: Wellington, New Zealand
Distribution: Slackware
Posts: 468

Rep: Reputation: 127Reputation: 127
It appears to work for me, with a few slight changes:

Line 15

Code:
$FINDALL = FALSE
should read
Code:
FINDALL=FALSE
and Line 18
Code:
FINDALL = TRUE
should read
Code:
FINDALL=TRUE
Other wise it did seem to work insofar as I entered
Code:
boygenius.sh python
and got
Code:
/usr/bin/python
in return. Didn't really do any testing beyond that.

As grail points out though, you really should use this markup when pasting in code:
[code]
code goes here
[/code]

Last edited by notKlaatu; 03-03-2014 at 04:18 PM. Reason: trying to get &#91; and &#93; to render as [ ] without actually getting a [code] tag
 
1 members found this post helpful.
Old 03-03-2014, 08:44 PM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,899

Rep: Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096
It is forgivable to have not done so the first time, but to ignore the request and do it again is not. Please go back and edit both posts and place your code in code tags.

I for one will not read something that is not formatted and as was pointed out by catkin, this simple process would have led you to find easily fixable issues.
 
Old 03-04-2014, 09:39 AM   #10
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,855

Rep: Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957
Regardless of the post formatting, the OP has shown adherence to the forum rules about posting homework, has shown effort of their own, and has been polite throughout. More posters should follow that example, in my opinion.
 
Old 03-04-2014, 10:58 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,899

Rep: Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096Reputation: 2096
And I am not chiding them for their good effort, but unfortunately with my old eyes the lack of contrast and formatting makes it difficult to assist
 
Old 03-04-2014, 11:02 AM   #12
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,857

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450
Indent is also extremely useful in looking at code. If it's not in code tags, I tend to not look too closely at it.
 
Old 03-04-2014, 01:54 PM   #13
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,157

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Re the post #8 by notKlaatu, one point made is that spaces are "tokenizers" in bash and Posex-compliant variants. (bash, itself, is not Posix-compliant.) Thus A = B is three "tokens," whilst A=B is one "token," and interpreted as an implicit let command.

The other point is that $ is a substitution command, and a command can't occur as the target of an assignment.
 
Old 03-04-2014, 02:00 PM   #14
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,431
Blog Entries: 1

Rep: Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144
Thats a great explanation.

To add to that... the reason bash tokenizes them using spaces is a variable called IFS which defaults to " \t\n" easier read as "(space)(tab)(newline)"
 
Old 03-04-2014, 02:06 PM   #15
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 15,855

Rep: Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957Reputation: 2957
Quote:
Originally Posted by grail View Post
And I am not chiding them for their good effort, but unfortunately with my old eyes the lack of contrast and formatting makes it difficult to assist
Believe me...I agree with you; I don't wear bifocals for fun. But I did want to give credit to the OP, especially given the MANY homework requests we see that are barely intelligible and/or posted verbatim from a book, with no effort at all.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
It 250 homework assignment chapter 5 nadida73 Linux - Newbie 3 10-18-2012 10:10 PM
would and could somebody help me with my homework assignment please... DarDevy Linux - Newbie 3 04-20-2009 02:43 PM
Newbie homework assignment longs_peak2002 Linux - Newbie 13 11-13-2007 11:19 PM
help me with my homework assignment by filling in the blank nkoplm General 11 11-25-2005 10:57 AM


All times are GMT -5. The time now is 10:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration