How to pass function parameter to do loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass function parameter to do loop?
# 1  
Old 05-26-2016
How to pass function parameter to do loop?

Hi All,

I have created one function for KSH and was running well with one parameter input since I just had to use $1 to get the parameter.
Now I want to do loop for each parameter(actually filenames) . I have try to use do loop, but $i does not resolve to parameter instead it resolves to 1, 2, 3 etc...

example,
Code:
func()
echo There are ${#} parameters..
i=1
while (( i <= ${#} ))
do
if  [ ! -f $("$i")];
then 
    echo "Error $i does not exist!"
else
echo "Error $i does exist, please proceed".
fi
     (( i+=1 ))
done
}

Please advice.
# 2  
Old 05-26-2016
Code:
#!/bin/ksh

function process_files
{
   echo There are ${#} parameters..
   k=1
   while [ $k -le ${#} ]
   do
      eval i=\${$k}
      if [ ! -f "$i" ]
      then
         echo "Error $i does not exist!"
      else
         echo "Error $i does exist, please proceed".
      fi
      (( k = k + 1 ))
   done
}


Last edited by rdrtx1; 05-26-2016 at 03:54 PM.. Reason: for files with spaces in the name.
This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 05-26-2016
Quote:
Originally Posted by rdrtx1
Code:
#!/bin/ksh
function process_files
{
   echo There are ${#} parameters..
   for i in $*
   do
      if [ ! -f "$i" ]
      then
         echo "Error $i does not exist!"
      else
         echo "Error $i does exist, please proceed".
      fi
   done
}

Hello rdrtx1,

I think in shell unlike awkthere is no need for function function_name, you could put directly as function_name {....}, so it could be written as follows.
Code:
#!/bin/ksh
process_files ()
{
   echo There are ${#} parameters..
   for i in $*
   do
      if [ ! -f "$i" ]
      then
         echo "Error $i does not exist!"
      else
         echo "Error $i does exist, please proceed".
      fi
   done
}

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-26-2016 at 04:07 PM.. Reason: added paranthesis
# 4  
Old 05-26-2016
Include parentheses in the other function definition format:
Code:
process_files ()

# 5  
Old 05-26-2016
Worked like charm. thanks!Smilie

---------- Post updated at 02:57 PM ---------- Previous update was at 02:41 PM ----------

Quote:
Originally Posted by rdrtx1
Code:
#!/bin/ksh

function process_files
{
   echo There are ${#} parameters..
   k=1
   while [ $k -le ${#} ]
   do
      eval i=\${$k}
      if [ ! -f "$i" ]
      then
         echo "Error $i does not exist!"
      else
         echo "Error $i does exist, please proceed".
      fi
      (( k = k + 1 ))
   done
}

It seems you edited and replaced with your previous answer. It worked for me. Thanks anyways!
# 6  
Old 05-27-2016
Note: the natural loop through the arguments is the for loop.
Code:
func(){
# the for loop defaults to 'in "$@"'
  for arg
  do
    if [ -f "$arg" ]
    then
      echo "Error '$arg' does exist, please proceed."
    else
      echo "Error '$arg' does not exist!"
    fi
  done
}
# pass all arguments of this shell to func()
func "$@"
func file1 file2 "filename with spaces"

Note that "$@" is preferrable to $* because it does not split on words.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to Dynamically Pass Parameter to plsql Function & Capture its Output Value in a Shell Variable?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: #! /bin/ksh v="ORG_ID" ... (2 Replies)
Discussion started by: sujitdas2104
2 Replies

2. Shell Programming and Scripting

How to pass position parameter into function.?

Hi Gurus, I have request which needs to pass position parameter to a function. I tried below simple code, it doesn't work. #!/bin/bash func_1(){ echo $1 } func_1 $ ./set_file abc $ do I need add some to get the position para first? thanks in advance. (3 Replies)
Discussion started by: ken6503
3 Replies

3. Shell Programming and Scripting

Pass parameter

Hi, I have following for loop , please let me know how to get ${TXP_EXT_TABLE_${i}_SQL} parameter with 1DAY and 7DAY values. for i in 1DAY 7DAY do ${NZSQL_DIR}/nzsql -h ${HOST} -time -v ON_ERROR_STOP=1 -f ${SQL_DIR}/${TXP_EXT_TABLE_${i}_SQL} > ${TMP_LOG_FILE} 2>&1 done ... (4 Replies)
Discussion started by: sandy162
4 Replies

4. Shell Programming and Scripting

Attempting to pass my array as a nested parameter to loop. A little lost.

I want to pass this array as a parameter. IFS=$'\n' fortune_lines=($(fortune | fold -w 30 )) Inside of this line screen -p 0 -S ${SCREEN_SESSION} -X stuff "`printf "say ${fortune_lines}\r"`" And I am lost at this point. I am thinking something like this? Then make it loop.. ... (7 Replies)
Discussion started by: briandanielz
7 Replies

5. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

6. Shell Programming and Scripting

Pass command as a function parameter

Hi guys, can someome help with this question, I have defined a function that takes a command as a parameter, but when the command is executed from the function it will throw errors because what I believe is a special character escaping issue. I tried using the backslash to escape the pipe | and >... (2 Replies)
Discussion started by: marouanix
2 Replies

7. Shell Programming and Scripting

How to pass parameter to User defined function in shell script?

Hello, Can anyone guide me tin passing parameters into user defined function of shell script (KSH). Here is my code, InsertRecord() { DB_TBL=$(sqlplus $USERID/$PASSWORD@$DATABASE << EOF set head off set feed off set serveroutput on INSERT INTO TBL1 ( OLD_VAL, NEW_VAL, ... (7 Replies)
Discussion started by: Poonamol
7 Replies

8. Shell Programming and Scripting

What is the maximum number of parameter we can pass to a shell script function?

what is the maximum number of parameter we can pass to a shell script function (8 Replies)
Discussion started by: alokjyotibal
8 Replies

9. Shell Programming and Scripting

pass parameter to function

HI all I have a code like ############################################## minyear() { curryear=$1 echo $curryear } ##Main Program ## minyear exit ####### when i execute "sh scriptname 2005" output should be like 2005 but the output is blank. I guess i need to pass parameter to... (3 Replies)
Discussion started by: vasuarjula
3 Replies

10. Shell Programming and Scripting

How to pass a parameter

Hi all, How to pass a parameter from a oracle pl/sql procedure parameter to shell environment and use it? (1 Reply)
Discussion started by: megh
1 Replies
Login or Register to Ask a Question