LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help Shellscript (https://www.linuxquestions.org/questions/linux-newbie-8/help-shellscript-322470/)

paraiso 05-11-2005 02:43 PM

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

paraiso 05-11-2005 02:46 PM

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
}


Tinkster 05-11-2005 03:19 PM

You're making assignments, not comparisons.


Cheers,
Tink

looseCannon 05-11-2005 03:20 PM

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.

paraiso 05-11-2005 03:58 PM

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
}


urka58 05-11-2005 04:40 PM

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)

paraiso 05-11-2005 06:06 PM

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


looseCannon 05-12-2005 07:25 AM

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.


All times are GMT -5. The time now is 09:30 PM.