Arrays and functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arrays and functions
# 8  
Old 08-09-2012
I get this.
Code:
$ ./fator.sh 1 2 3 4
1 2 3 4
No of arguments: 4
length of array is: 4
k[0] = 1
a[0] =
k[1] = 2
a[1] = 2
k[2] = 3
a[2] =
k[3] = 4
a[3] =

$

What am I supposed to be seeing here?

You didn't need arrays to do any of that, fyi.
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 08-09-2012
Your problem was here:
Code:
a=("${a[$i]}" "$(fator ${k[$i]})")

Should be a=(${a[@]} "$(fator ${k[$i]})") ... or why not a[i]=$(fator ${k[i]})

Here is a bit of a cleanup to use bash expressions and for loops everywhere instead of while and expr:

Code:
#!/bin/bash
fator() {
  ((j=$1+1))
  z=1
  f=0
  for (( p = 1; $p < $j; p++ )) {
    ((f=j-p))
    ((z=z*f))
  }
  echo "$z"
}
 
k=("$@")
echo ${k[*]}
echo "No of arguments: $#"
echo "length of array is: ${#k[*]}"
for ((i=0;i<${#k[*]};i++)) {
   echo "k[$i] = ${k[i]}"
   a[i]="$(fator ${k[i]})"
   echo "a[$i] = ${a[i]}"
}
echo ""

Again as as Corona688 said there is no real need for arrays here unless you plan to use them later on in the script.

Last edited by Chubler_XL; 08-09-2012 at 07:09 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 10  
Old 08-10-2012
Array is needed for further using as input values.

---------- Post updated at 01:27 PM ---------- Previous update was at 01:24 PM ----------

what is the technical difference in arrays, a[@] and a[*].
besides you have omitted the $ signs to make things tidy, however, I am lacking adequate knowledge on rules. Any links!
Thanks!
# 11  
Old 08-13-2012
some issues with file renaming with arrays

Hi! guys! again with some problem solving issue. The problem is that I have few files in a folder some are with extension .txt and some are not so how to put them all with .txt extension minus the shell script which i usually put in the same folder. I tried making it out, but there is some problem. Here is the code and details below.

Code:
#!/bin/bash
# program for file renaming
echo "Enter path: "
read p # pattern should be *.txt, *.pdf or simply *
a=($p)

for (( i = 0; i < ${#a[*]}; i++ ))
do
echo "a[$i] = ${a[$i]}"
done

echo "Enter the file format: "
read f   # can be given as txt or pdf

for (( i = 0; i < ${#a[*]}; i++ ))
do
for (( ${a[$i]} != "frn2c" ))
do
v[$i]=("$(cat ${a[$i]} | sed 's/\(.\)/\1 \n/g')")
echo ${v[$i]}
c[$i]=("$(mv ${v[$i]} ${v[$i]}.$f)")
echo ${c[$i]}
done
done

And, the problem is denoted with this line
Code:
for (( ${a[$i]} != "frn2c" ))

The error is
Code:
./frn2c: line 17: syntax error: arithmetic expression required
./frn2c: line 17: syntax error: `(( ${a[$i]} != "frn2c" ))

# 12  
Old 08-13-2012
It means what it says. (( )) brackets are used for arithmetic expressions. If you want to do things like comparing strings, use [[ ]].

[[ ${a[$i]} != "frn2c" ]]
This User Gave Thanks to Corona688 For This Post:
# 13  
Old 08-13-2012
You can't use for [[ ... ]], can you? The requestor should use if / while [[ ... ]] to run the conditional branch.
This User Gave Thanks to RudiC For This Post:
# 14  
Old 08-13-2012
In continuation

Thanks! the square braces did the trick, now here is one more thing!
Compare the two scripts, the first one gives the results, the second one does not.

Code:
#!/bin/bash
# program for data mining

echo "This programs read text files and brings out the"
echo "word count of first 50 most used words in a file."

a=(*.txt)

for (( i = 0; i < ${#a[*]}; i++ ))
do
echo "a[$i] = ${a[$i]}"
done
echo ""


for (( i = 0; i < ${#a[*]}; i++ ))
do
printf "=======================" >> lsf2dout.txt
printf "\n" >> lsf2dout.txt
printf "Name of the file: ${a[$i]}" >> lsf2dout.txt
printf "\n" >> lsf2dout.txt

cat ${a[$i]} | sed 's/\([[:space:]]\)/\1 \n/g' | sed 's/^[ \t]*//' | sed \
's/[ \t]*$//' | sed '/^$/d' | sed 's/^to$//g' | sed 's/^of$//g' | sed \
's/^and$//g' | sed 's/^a$//g' | sed 's/^the$//g' | sed \
's/^The$//g' | sed 's/^is$//g' | sed 's/^in$//g' | sed 's/^as$//g' | sed \
's/^for$//g' | sed 's/^by$//g' | sed 's/^on$//g' | sed 's/^at$//g' | sed \
's/^be$//g' | sed 's/^an$//g'|sed 's/^it$//g' | sed 's/^no$//g' | sed 's/^It$//g' | sed \
's/^us$//g' | sed 's/^are$//g' | sed 's/^was$//g' | sed 's/^up$//g' | tr \
-cd '[:alnum:] [:space:]'| sort | uniq -c | sort \
-nr | head -n 50 >> lsf2dout.txt

printf "\n\n" >> lsf2dout.txt
done

and the modified one below

Code:
#!/bin/bash
# program for data mining

echo "This programs read text files and brings out the"
echo "word count of first 50 most used words in a file."

a=(*.txt)

for (( i = 0; i < ${#a[*]}; i++ ))
do
echo "a[$i] = ${a[$i]}"
done
echo ""


for (( i = 0; i < ${#a[*]}; i++ ))
do
printf "=======================" >> lsf2dout.txt
printf "\n" >> lsf2dout.txt
printf "Name of the file: ${a[$i]}" >> lsf2dout.txt
printf "\n" >> lsf2dout.txt

cat ${a[$i]} | sed 's/\([[:space:]]\)/\1 \n/g' | sed 's/^[ \t]*//' | sed \
's/[ \t]*$//' | sed '/^$/d' | 
for (( i = 0; $i <= ${#g[*]}; i++ )) 
do 
sed 's/^${g[$i]}$//g'
done | tr -cd '[:alnum:] [:space:]'| sort | uniq -c | sort -nr | head -n 50 >> lsf2dout.txt

printf "\n\n" >> lsf2dout.txt
done


declare -a g=('and' 'us' 'the' 'The' 'we' 'is' 'a' 'at' 'to' 'of' 'is' 'in' 'as' 'be' 'for' 'by' 'on' 'us' 'are' 'was' 'up')

CANT LOCATE WHERE I HAVE GONE WRONG.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

2. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

3. Shell Programming and Scripting

i think i need functions ?

Hi, im making a little script but need some help Code i have so far is read -p 'Bot Nickname:' ecnick read -p 'Bot Username:' ecusername read -p 'Bot Realname:' ecrealname read -p 'Your Email:' ecemail echo '' echo Your bots nickname is set to $ecnick echo Your bots username is set to... (2 Replies)
Discussion started by: Gemster
2 Replies

4. UNIX for Dummies Questions & Answers

Help with functions

Hi, I am exploring with defining functions in my BASH shell scripts. However, I am bit confused about how to pass parameters to my functions. I was under the impression that you must do something like the following: Define a function called "sample_function": function sample_function {... (3 Replies)
Discussion started by: msb65
3 Replies

5. Shell Programming and Scripting

Need a little help with functions

I'm semi new to unix/linux and am trying to convert a program I wrote in C++ to a bash script. It's a program that prints Fibonacci's series. I have found scripts that will do it, but I'm trying persistently to get this one to work. The problem occurs when I try to return a value from the function.... (3 Replies)
Discussion started by: Glowworm
3 Replies

6. Shell Programming and Scripting

functions

I have korn shells where I want to create a function passing $1 to a function , determine my $STAT_ENV value, set the paths and return the paths for STATSH,STATPRM,STATSQR,STATSQL,STATCTL TO BE USED IN THE UNIX SCRIPT THE CALLED THE fucnction in the first place. Can someone tell me the best... (2 Replies)
Discussion started by: TimHortons
2 Replies

7. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

8. Shell Programming and Scripting

Passing arrays between functions

Hi, I have a function that hold 3 arrayies. I need to pass them to another function as an input, for further use Could you please explain how to do that. Thanks (5 Replies)
Discussion started by: yoavbe
5 Replies

9. Shell Programming and Scripting

perl functions and arrays

Hi, First I will tell my objective of this function (function one). I have a table for ex: id passwd name -- ------ ----- 1 fdhgfs werwer 2 fsdfs sdfsdf 3 sdfs sdfsdf 4 fdsfs dssdf . . . . . . The id, passwd and name are the arguments for another function say two. (1 Reply)
Discussion started by: mercuryshipzz
1 Replies

10. Shell Programming and Scripting

Use of functions

Hi my shell is tcsh can I have functions in my shell scripting? Is the below shell script correct. Can I have two functions and call one of them as required. ---------- echo "functions" f1 f1 () { echo "hello" } f2 () (1 Reply)
Discussion started by: amitrajvarma
1 Replies
Login or Register to Ask a Question