Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: call function in bash  (Read 10011 times)

0 Members and 1 Guest are viewing this topic.

mala_un

  • Guest
call function in bash
« on: August 15, 2006, 02:58:12 AM »
hi all,

i have a problem, i did search around but end up disappointed.

lets say i have 3 scripts script_a, script_b and script_c.
all this script is saved in the same directory
what if i want to call script_a and script_b inside a script_c

i've test this way;

#!/bin/bash
clear
if [ -e filename ]
./script_a
else
./script_b
fi
exit

but then, i cant call the ./script_a and ./script_b.
the error message show that there is unexpected before else.

how am i going to do this?

Rob Pomeroy



    Prodigy

  • Systems Architect
  • Thanked: 124
    • Me
  • Experience: Expert
  • OS: Other
Re: call function in bash
« Reply #1 on: August 15, 2006, 11:06:36 AM »
Try this:
#!/bin/bash
clear
if [ -e filename ]
  /bin/bash /fullpathname/script_a
  exit
else
  /bin/bash /fullpathname/script_b
  exit
fi
exit
Only able to visit the forums sporadically, sorry.

Geek & Dummy - honest news, reviews and howtos

mala_un

  • Guest
Re: call function in bash
« Reply #2 on: August 15, 2006, 06:14:44 PM »
thanks rob..

i will try

mala_un

  • Guest
Re: call function in bash
« Reply #3 on: August 15, 2006, 09:50:45 PM »
its working man..
u r great  ;D

mala_un

  • Guest
Re: call function in bash
« Reply #4 on: August 15, 2006, 10:20:33 PM »
another problem...

i want user to enter a file name..
then i want to check whether the file is exist or not in home directory..
if exist.. copy to new file..

i wrote this way, but its not working...

#!/bin/bash
echo "Enter filename:"
read filename

cd /home/
if [ -e $filename ]
then
cp $filename /home/script/filename2
else
echo "The file not exist"
fi

the result: unexpected .... before else

ghostdog74



    Specialist

    Thanked: 27
    Re: call function in bash
    « Reply #5 on: August 16, 2006, 02:50:28 AM »
    hmm.strange, your code works on my machine, which i only change the path to my own path.
    the thing i can suggest is to make sure your paths are correct...etc...
    also, for debugging, you could try to put set -x in your script, eg

    #/bin/bash
    set -x
    ...
    ...
    <your code>
    ...

    This will show some debug statements in your output...

    mala_un

    • Guest
    Re: call function in bash
    « Reply #6 on: August 16, 2006, 04:24:31 AM »
    hehe
    what a shame... it was my fault..

    i should write this way

    #!/bin/bash
    echo "Enter filename:"
    read filename
     
    if [ -e $filename ]
    then
    cp /home/$filename /home/script/filename2
    else
    echo "The file not exist"
    fi

    thanks ghostdog74

    mala_un

    • Guest
    Re: call function in bash
    « Reply #7 on: August 16, 2006, 07:49:42 PM »
    i'm a bit confuse...

    if i have something like this,
    ./var $1 $2 $3 $4 $5 $6 $7 $8 $9

    what would it used for actually..

    ghostdog74



      Specialist

      Thanked: 27
      Re: call function in bash
      « Reply #8 on: August 16, 2006, 08:15:00 PM »
      assume your script name is "test.sh" and has something like this:

      #!/bin/bash
      ...
      ./var $1 $2 $3 $4 $5 $6 $7 $8 $9
      ....

      this means, you are passing arguments from 1 to 9 passed to "test.sh" to the script called "var" at the current directory.

      So assuming test.sh is a script  to store 9 user information, and user has these information
      1) name = mala
      2) tel = 12345678
      3) house no = 100
      4) favorite color = black
      5) favorite number = 4
      6) birthday = 12082006
      7) car = mercedes benz
      8) sex = male
      9) city = somewhere

      so when invoking the "test.sh" script, it look like this maybe:

      unix#>  ./test.sh mala 12345678 100 black 4 12082006  "mercedes benz" male somewhere

      and then all these arguments will be passed to "var" script in your "test.sh" script...
      man, my english is not very good, hope you understand. :)


      mala_un

      • Guest
      Re: call function in bash
      « Reply #9 on: August 16, 2006, 11:37:32 PM »
      you r doing well man,

      let me try, correct me if i'm wrong

      so the ./var is another script to store all the data into
      1) ..
      to
      8)
      9)
      is it?

      what about this.. am i correct?

      ./mala $1 $2 $3 $4 $5 $6 $7 $8 $9

      if [ $? = 0 ]; then

      echo "success"

      fi

      does this mean that, if ./mala run successfully without error, it will return 0.
      if there is an error, it will come maybe from $5.. or else..

      am i right?

      i just want to know if there is an error.

      ghostdog74



        Specialist

        Thanked: 27
        Re: call function in bash
        « Reply #10 on: August 16, 2006, 11:56:35 PM »
        Quote

        so the ./var is another script to store all the data into
        1) ..
        to
        8)
        9)
        is it?

        what about this.. am i correct?


        yes, basically, var is used to process those arguments passed from the first script.


        Quote
        ./mala $1 $2 $3 $4 $5 $6 $7 $8 $9

        if [ $? = 0 ]; then

        echo "success"

        fi

        does this mean that, if ./mala run successfully without error, it will return 0.
        if there is an error, it will come maybe from $5.. or else..

        am i right?

        That will depend on how "mala" script is written.An example for "mala" script

        Code: [Select]
        #!/bin/sh
        name=$1
        tel=$2
        ....
        ....

        if [ $tel="" ];then
           exit 1
        fi

        ...
        ...

        Then from the first script,  you check the return code

        Code: [Select]
        if [ $? -eq 1 ];then
            echo "telephone number is invalid"
        fi

        Similarly for other errors , you can assign an exit code...

        This is just an example..




        mala_un

        • Guest
        Re: call function in bash
        « Reply #11 on: August 17, 2006, 12:24:04 AM »
        thanks man..

        it very nice of u to explain to me clearly..

         ;D

        ghostdog74



          Specialist

          Thanked: 27
          Re: call function in bash
          « Reply #12 on: August 17, 2006, 01:01:39 AM »
          no problem  :)

          mala_un

          • Guest
          Re: call function in bash
          « Reply #13 on: August 17, 2006, 02:22:24 AM »
          oh man...
          my head want to explode...

          /home/script_a : [ : blog.txt : binary operator expected

          from this error what can u see...

          it just cannot open the blog.txt..


          ghostdog74



            Specialist

            Thanked: 27
            Re: call function in bash
            « Reply #14 on: August 17, 2006, 03:00:46 AM »
            not really, is script_a a shell script ? or a compiled program?
            please post the script content if possible.