On performing SHELL scripting - facing Syntax error: "(" unexpected : "Faced with several other try with shell scripting with function definition"


pract3.sh: 1: Syntax error: “(” unexpected

For all below program facing this - common error while getting started refreshing shell programming:( Any reason or mis-leaded executions - please confirm)
==========For below line of code on pract3.sh=======================

function  user_input()
{
        read -p "Please enter cost center" costcenter
}

=============pract2.sh==========
ash

function get_salestax()
{
        if test "$1" = ""
        then
                echo "You must supply a full expense amopunt."
                return
        else
                echo "Full expense amount:" $1
                nontax_amount=`echo "$1/1/15" | bc`
                tax_amount=`echo "$1-$nontax_amount" | bc`
                echo "Non tax amount:"$nontax_amount
                echo "Tax amount:" $tax_amount
                return $tax_amount

        fi
        echo "Operation completed successfully!"
}

====================================================

மேலே கொடுக்கப்பட்டுள்ள தகவல்கள் சரியாக புரியவில்லை. ctrl-alt-t கொடுத்து டெர்மினலை துவக்கவும். பின் எந்த ஷெல்ஸ்கிரிப்டில் பிழை வருகின்றதோ அந்த ஷெல்ஸ்கிரிப்டை இயக்கவும். சிக்கல் வந்தபில் டெர்மினல் திரையை திரைப்பதிவு எடுக்கவும். பின் அதை இங்கே பகிரவும்.

இந்த சிக்கல் தொடர்கின்றதா என்று கூறவும்.


சிக்கல் தொடர்கிறது.

  1. இதை இயக்கினால் என்ன வருகின்றது?
  2. என்ன வர வேண்டும் என்று எதிர்பார்கின்றீர்கள்?

After executing, I am expecting to execute without error and like to get an input after execution of above code. I never tried functions with shell, while experimenting with functions got this problem.

With an another instance, seen different problem with execution on shell scripting.
Error: pract4.sh: 8: Bad substitution ==> Was observed.
Solution found: When I try to execute from $PS1 ==> Worked ( Attached the observed copy)

Since, shell scripting - I have worked on basic- was advancing with experimenting with functions() ==> Seen persistent problems - That’s why like to ask for clarifications:

can you sent the program inside the pract4.sh file

#!/bin/bash
find_average() {
sum=0
i=1
len=$#
x=$((len + 1))
while [ $i -lt $x ]
do
arg=${!i}
sum=$((sum + arg))
i=$((i + 1 ))
done
avg=$((sum / len))
return $avg
}
find_average 10 20 30 40
echo $?

After executing, I am expecting to execute without error and like to get an input after execution of above code. I never tried functions with shell, while experimenting with functions got this problem.

ஒவ்வோரு ஷெல்ஸ்க்ரிப்டாக செல்வோம். pract3.sh ஐ இயக்கியவுடன் அந்த டெர்மினலை திரைப்பதிவு எடுத்து இங்கே பகிரவும்.

In this code arg = ${!i} is not working to get value from find_average 10 20 30 40.

use this code, I use for loop and get the output of this statement

find_average(){ 
  len=$#
  sum=0
  for x in "$@"
  do
     sum=$((sum + x))
  done
  avg=$((sum/len))
  return $avg
}

find_average 10 20 30 40
echo "$?" 

while perform ${!i} this, we need to give permission for that file.
Give permission then use execute then the file

chmod u+x filename

then run

./filename

My LINUX system is using bash as default SHELL,
so, on executing with bash <script.sh> ==> got properly executed.

box@box-virtual-machine:~/shell_prog$ sh pract4.sh
pract4.sh: 9: Bad substitution
box@box-virtual-machine:~/shell_prog$ bas
base32 base64 basename basenc bash bashbug
box@box-virtual-machine:~/shell_prog$ bash pract4.sh
25
box@box-virtual-machine:~/shell_prog$

Thanks kalai and mohan for responding :wave:

1 Like