I have the following code, that I want to be inside an function getsum(). I tried with the following code working without the function. When I run ./sum 5 6 I get 11.
#!/bin/bash
sum=0
for i in $#; do sum=$((sum+i)); done
echo $sum
exit 0
But how can I put it in a function doing the same job?
I tried the following code but it doesn't work.
#!/bin/bash
sums() {
sum=0
for i in $#; do sum=$((sum+i)); done
echo $sum
exit 0
}
sums
You just need to pass the arguments ($#) to the function sum() that you pass to your script:
#!/bin/bash
sums() {
sum=0
for i in $#; do sum=$((sum+i)); done
echo $sum
exit 0
}
sums "$#" # Note this line
Related
I am trying to create a shell script using another script.
Following is the code.
#!/bin/bash
count=$#
cat << EOF > /tmp/kill_loop.sh
#!/bin/bash
while true;
do
for i in "$#"
do
echo $i
done
done
EOF
When I see kill_loop.sh , "$i" is empty.
#!/bin/bash
while true;
do
for i in "one two three"
do
echo
done
done
I want "$i" to be printed as such in kill_loop.sh file so that if i execute kill_loop.sh, it echoes the value 'one','two' and 'three'
Your "outer" shell script is interpreting $i as if it were one of its own variables, which isn't set, thus it evaluates to nothing. Try escaping the $ so the outer shell doesn't expand it:
echo \$i
here is the "foreach" function:
function foreach
{
typeset cmd=$1;
shift;
for arg in "$#";
do
$cmd $arg;
done
}
I have a function which takes another function as an argument and perform some operation.
e.g.
processFunc()
{
Func=$1
...
}
Now the problem is when i am passing a function which also accept multi line variable as argument.
e.g.
VAL="1\n2\n3\n4"
Func1()
{
VAL=$1
...
}
Now, there is no problem when I call direct function Func1()
e.g.
VAL2=`Func1 "$VAL"`
But an issue arises when I try to call proccessFunc()
e.g.
VAL3=`processFunc 'Func1 "$VAL"'` #Here is the problem...
Need help how to resolve the above problem.
Test code
#! /bin/bash
#set -x
func1()
{
VAL1="$1"
echo "$VAL1" ok
for i in $VAL1
do
echo $i
sleep 1
done
}
func2()
{
VALFUN="$1"
TIME=$2
COUNTER=1
while [ $COUNTER -le $TIME ]
do
# clear
$VALFUN
sleep 2
COUNTER=`expr $COUNTER + 1`
done
}
func3()
{
echo a
echo b
echo c
}
#### Main #####
LOCALVAL="`echo -e "123\nabc\nxyz"`"
#echo "$LOCALVAL"
echo "func1"
func1 "$LOCALVAL"
echo "func2 with func3"
func2 "func3" 3
echo "func2 wth func1"
func2 "func1 "$LOCALVAL"" 3
Error I am getting is:
./test.sh: line 18: [: abc: integer expression expected
Wouldn't that be great if you use the output from the Func1 and use it for the processFunc
So your code would be :
VAL="1\n2\n3\n4"
VAL2=Func1 "$VAL"
Where Func1 would be :
Func1()
{
VAL=$1
...
}
And then calling the processFunc as
VAL3=processFunc "$VAL"
Hope this helps.
Or Else what I suppose is that you are passing a string value where your shell is expecting a string.
And I believe the code should be :
a=123\nabc\nxyz
LOCALVAL = echo $a
You can do like this
VAL3="processFunc Func1 $VAL"
$VAL3
I was trying things with bash scripts. I made this simple script
#!/bin/bash
function myfun()
{
for item in `seq 1 5`
do
echo "$item $1 $2"
done
}
myfun
but no luck. If I change it like this as below, everything seems to be fine,
#!/bin/bash
a=$1
b=$2
function myfun()
{
for item in `seq 1 5`
do
echo "$item $a $b"
done
}
myfun
It looks like arguments (positional parameters) do not work inside function in shell. Am I doing any mistake? I am still learning things. So can you explain why is it so?
It's the function not the loop:
function myfun()
{
for item in `seq 1 5`
do
echo "$item $1 $2"
done
}
# Pass all of the script's parameters to the function,
# as if writing myfun "$1" "$2" "$3"..
myfun "$#"
Inside a function, $1 and $2 give the function arguments and not the script parameters.
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html
I have a problem with writing it in bash... I know how it works in C++, but I have trouble implementing it in bash. Here's what I got:
sum()
{
let minusOne=$1-1
let result=sum $minusOne +$1
}
You need an exit condition. In bash, $((...)) is arithmetic expansion, and $(...) is command substitution (see the man page).
sum() {
if (( $1 == 1 )); then
echo 1
return
fi
local minusOne=$(( $1 - 1 ))
echo $(( $1 + $(sum $minusOne) ))
}
A non-recursive way to write a sum function:
sum() {
set -- $(seq 1 $1)
local IFS=+
echo "$*" | bc
}
Here is a function that will give you a sum of numbers provided as arguments. The following prints "10":
#!/bin/bash
sum() {
local total=0
for number in "$#"; do
(( total += number ))
done
echo $total
}
sum 1 2 3 4
How to pass array as function in shell script?
I written following code:
function test(){
param1 = $1
param2 = $2
for i in ${$param1[#]}
do
for j in ${param2[#]}
do
if($(i) = $(j) )
then
echo $(i)
echo $(j)
fi
done
done
}
but I am getting line 1: ${$(param1)[#]}: bad substitution
There are multiple problems:
you can't have spaces around the = when assigning variables
your if statement has the wrong syntax
array passing isn't right
try not to call your function test because that is a shell command
Here is the fixed version:
myFunction(){
param1=("${!1}")
param2=("${!2}")
for i in ${param1[#]}
do
for j in ${param2[#]}
do
if [ "${i}" == "${j}" ]
then
echo ${i}
echo ${j}
fi
done
done
}
a=(foo bar baz)
b=(foo bar qux)
myFunction a[#] b[#]
You can use the following script accordingly
#!/bin/bash
param[0]=$1
param[1]=$2
function print_array {
array_name=$1
eval echo \${$array_name[*]}
return
}
print_array param
exit 0
A simple way :
function iterate
{
n=${#detective[#]}
for (( i=0; i<n; i++ ))
do
echo ${detective[$i]}
done
}
detective=("Feluda" "Sharlockhomes" "Bomkesh" )
iterate ${detective[#]}