LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-11-2005, 02:43 PM   #1
paraiso
Member
 
Registered: Apr 2005
Distribution: Fedora Core 4
Posts: 88

Rep: Reputation: 15
Unhappy Help Shellscript


I am trying to write shellscript which could read a path, go through the content of the path, determine the type for each file under the directory path and finally write the result to a registry file. My code doesn't work and I don't know where the mistake is... help !


Code:
 
#!/bin/bash

echo C: Create file registry
read choice

if [[ $choice = c || $choice = C ]]
 then
   echo -n "Enter path: "
   read path
   createRegistry path
fi
 
function createRegitry ()
{
  read $1
  file $1/* | tee registry_file
}
Cheers
 
Old 05-11-2005, 02:46 PM   #2
paraiso
Member
 
Registered: Apr 2005
Distribution: Fedora Core 4
Posts: 88

Original Poster
Rep: Reputation: 15
Sorry I paste the wrong code (though anyway this one is also wrong )

Code:
 
#!/bin/bash
 
 
 
echo C: Create file registry
echo P: Print files of the specified type
echo R: Remove specified files from registry
echo Q: Quit
echo -n "Make a choice: "
read choice
 
if [[ $choice = c || $choice = C ]]
 then
   echo -n "Enter path: "
fi          
                      
function createRegitry()
{ 
  read x
  file $x/* | tee registry_file
}
 
Old 05-11-2005, 03:19 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
You're making assignments, not comparisons.


Cheers,
Tink
 
Old 05-11-2005, 03:20 PM   #4
looseCannon
Member
 
Registered: Dec 2003
Location: Little Rock, AR
Distribution: Fedora Core 2, AIX, HP-UX, Solaris, Whitebox
Posts: 193

Rep: Reputation: 31
There is a simpler way to do what you are trying to do. Take a look at the code below:

Code:
#!/bin/bash

PS3='Enter choice : '
select CHOICE in 'Create file registry' \
                 'Print files of the specified type' \
                 'Remove specified files from registry' \
                 'Quit'
do
         case CHOICE in
                'Create file registry' )
                        echo 'Enter the path directory to search'
                        read DIRTOSEARCH
                        
                        for FILE in `/bin/ls $DIRTOSEARCH`
                        do
                                echo $file >> /tmp/registry_file
                        done
                break ;;
        esac
done
That might need some tweaking, but it gives you a general idea. The select statement will build a menu for you, instead of you having to create one with a bunch of echo statements.
 
Old 05-11-2005, 03:58 PM   #5
paraiso
Member
 
Registered: Apr 2005
Distribution: Fedora Core 4
Posts: 88

Original Poster
Rep: Reputation: 15
Thanks looseCannon for the select statement tips! I made it complicated because I wanted to exercise with functions, but I get these kind of mistake:

Enter path: project1: createRegistry: command not found

Code:
 

#!/bin/bash
 
echo C: Create file registry

echo -n "Make a choice: "
read choice
 
if [[ $choice = c || $choice = C ]]
 then
   echo -n "Enter path: "
   createRegistry
fi
 
function createRegitry()
{ 
  read x 
  file $x/* | tee registry_file
}
 
Old 05-11-2005, 04:40 PM   #6
urka58
Member
 
Registered: Nov 2003
Distribution: slackware 14
Posts: 542

Rep: Reputation: 32
I did not check if this script can work, but it seems you are asking the shell to execute a function (basically a command) that will be created later....
Hope this helps
Ciao

PS in the function definition you wrote createRegitry (there is an "s" missing anyway)

Last edited by urka58; 05-11-2005 at 04:46 PM.
 
Old 05-11-2005, 06:06 PM   #7
paraiso
Member
 
Registered: Apr 2005
Distribution: Fedora Core 4
Posts: 88

Original Poster
Rep: Reputation: 15
Thanks urka58! and all of you. Yeap! didn't see the "s" missing, time to have a break now! I though I could create a function wherever in the code like in for instance Java, well now I know, thanks


Code:
#!/bin/bash
 
echo C: Create file registry
echo -n "Make a choice: "
read choice
 
function createRegistry()
{
  read x
  file $x/* | tee registry_file
}
 
 
if [[ $choice = c || $choice = C ]]
 then
   echo -n "Enter path: "
   createRegistry
fi
 
Old 05-12-2005, 07:25 AM   #8
looseCannon
Member
 
Registered: Dec 2003
Location: Little Rock, AR
Distribution: Fedora Core 2, AIX, HP-UX, Solaris, Whitebox
Posts: 193

Rep: Reputation: 31
Another thing I'll throw in...

if [[ $choice = c || $choice = C ]]

I've never been able to get an OR to work in ksh. You're using bash, so it might work. Generally I use the 'tr' command to translate things to either uppercase or lower case.

Something like this:

Code:
UPPERCASE=`echo $choice|tr [a-z] [A-Z]`
LOWERCASE=`echo $choice|tr [A-Z] [a-z]`
When it is all done UPPERCASE will have an uppercase version of $choice, and LOWERCASE will have a lowercase version of $choice.

Last thing: the general rule with functions is to put them at the top of your script. This is because the scripts are executed a line at a time and a function at the bottom won't be seen until the very end of the script. When you put it at the top it is loaded into memory and can be used at any point during the script.
 
  


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
Lockscreen Shellscript 2 paraiso Linux - General 1 05-11-2005 05:30 PM
Table format with ShellScript paraiso Linux - General 2 05-11-2005 06:39 AM
shellScript help paraiso Linux - Newbie 2 05-10-2005 07:09 PM
LS and shellscript - I'm stuck MikeHunt79 Linux - Software 2 03-11-2005 04:44 AM
ShellScript sharadgana Programming 1 12-16-2004 01:16 PM


All times are GMT -5. The time now is 10:52 AM.

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