Passing alias to a function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing alias to a function
# 1  
Old 11-20-2012
Hammer & Screwdriver Passing alias to a function

The objective of this function is to validate the file full path.

Code:
cat /dev/null > crontab_NOTEXISTS.txt

function File_Existence # Accepts 1 parameter
{
file_name="$(echo $1)"
echo "${file_name}"
[ -f $file_name ] && break || echo "$file_name NOT FOUND" >> crontab_NOTEXISTS.txt
}

while read file_name
do
  File_Existence ${file_name}
done < crontab_FILELISTS.txt

The problem is that, when the file directory is denoted by an alias, in this example $ORACLE_HOME=/oracle/ora9.2, the function would accept the $ORACLE_HOME as a raw string rather than an alias.

So, how do I solve this problem?

Let's say I have this in my crontab_FILELISTS.txt:
Code:
$ORACLE_HOME/test1.text
/oracle/ora9.2/test1.text

The file exists and both directory also valid. But if I run the script, it will output:
Code:
$ORACLE_HOME/test1.text NOT FOUND

Please help.

Thank you.
# 2  
Old 11-20-2012
Quote:
Originally Posted by aimy
$ORACLE_HOME
is your $ORACLE_HOME defined before you call your script.

You should define this variable first.

try sth like this.

Code:
[ -f $(eval echo $file_name) ] && echo pass || echo fail

# 3  
Old 11-20-2012
First, you need to make sure ORACLE_HOME is known to the script by exporting it.
Second, try this in the script: eval File_Existence ${file_name}
You may know that the use of eval can be unpredictable and dangerous and is discouraged by some people in this forum.

@pamu: Congrats, you were faster!

Just for info - you are using a shell variable. An alias is sth. different, see man page

Last edited by RudiC; 11-20-2012 at 05:21 AM.. Reason: Info on alias/variable
# 4  
Old 11-20-2012
Hi aimy!
First, I think it's not necessary to do an "echo" in file_name.
As Rudic says you must to export you ORACLE_HOME first. Then you only have to pass the name of file to function. For example:
Code:
cat /dev/null > crontab_NOTEXISTS.txt 
export ORACLE_HOME="..."


function File_Existence # Accepts 1 parameter 
{ 
file=$ORACLE_HOME/$1 
echo "${file}" 
[ -f $file ] && break || echo "$file NOT FOUND" >> crontab_NOTEXISTS.txt 
}  
while read file_name
do   
     File_Existence ${file_name} 
done < crontab_FILELISTS.txt

I don't know the format of file and if ORACLE_HOME is in crontab_FILELISTS.txt, if ORACLE_HOME is in file then erase $ORACLE_HOME/$1, just use $1.

Last edited by danielos; 11-20-2012 at 06:56 AM..
# 5  
Old 11-21-2012
Hi all.

Thanks a lot for helping.

FYI, the $ORACLE_HOME is already set in the profile.

In fact, if I issue this command, it will work fine..
Code:
File_Existence $ORACLE_HOME/test1.text

So, where did I went wrong?

Thank you.

---------- Post updated at 02:56 PM ---------- Previous update was at 08:46 AM ----------

Anyone? Smilie

I've tried manipulating the script so many times but to no avail Smilie

Please help.

Thank you.
# 6  
Old 11-21-2012
Have you tried using eval...?
# 7  
Old 11-21-2012
Quote:
Originally Posted by pamu
Have you tried using eval...?
Sorry.. Seems that eval has solved my problem.

I just worried when Rudic said that it is dangerous.. Smilie

Honestly, I did not know anything about eval all these while.

Thank you very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing an argument using alias to piped command

Hi. I'm trying to do a "simple" thing. grep -rls grepped_exp path | xgs where xgs is an alias to something like: xargs gvim -o -c ":g/grepped_exp" now the problem is that I want to pass the "grepped_exp" to the piped alias. I was able to do something like what I want without the... (4 Replies)
Discussion started by: hagaysp
4 Replies

2. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

3. Shell Programming and Scripting

awk alias passing a value to a variable

I am trying to turn this into an alias with no luck. I would then like to put the alias into my bashrc file. I know awk is very picky about quotes. I have tried every version of quotes, single quotes, double quotes, and backslashes that I can think of. VAR=$(xrandr | awk '$2=="connected"{s=$1}... (3 Replies)
Discussion started by: cokedude
3 Replies

4. UNIX for Dummies Questions & Answers

Passing variable to Alias in Hp kshell

Hi all, I have a series of directories which i open regularly. I want create an alias so that i can pass the direcotry name to alias and then this commands makes Cd to the path i need. COuld you please help on how to create an alias ex of what i am trying but couldn't succeeded #alias... (1 Reply)
Discussion started by: firestar
1 Replies

5. UNIX for Dummies Questions & Answers

Passing arguments to alias with multiple commands

I have a few aliases set up on AIX servers in my .kshrc file. Some of them contain multiple commands that are piped together. A simple example would be something like this: # alias to list directory contents as root and sort by size. alias lss='sudo ls -l | sort -nbk5' When I call... (5 Replies)
Discussion started by: derndingle
5 Replies

6. Shell Programming and Scripting

Passing two variables to function

HI ,I am a new in Bash and ,I dont know how to pass a second parameter to this fuction,if the name of the passed argument is num works fine,but if I try to pass secondNum,dont recognized it thanks function check() { if(($(echo ${#num}) == 0 )) then echo No arguments passed.Try... (6 Replies)
Discussion started by: lio123
6 Replies

7. UNIX for Dummies Questions & Answers

Using * when passing argument to alias

I have some folders with this structure: /data/me/a123xxx Where "xxx" is some changing series of letters, and "123" changes so folders might look like: /data/me/a123xxx /data/me/a234ysd /data/me/a534sds etc. The numbers are what matter to me (they identify the folder), so I created an... (7 Replies)
Discussion started by: ramparts
7 Replies

8. UNIX for Dummies Questions & Answers

How to create alias for a function

Hi, In Unix (AIX/LINUX) how do we create a alias for a UNIX shell function? Please help me with that. Thanks!! (2 Replies)
Discussion started by: neeto
2 Replies

9. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

10. UNIX for Dummies Questions & Answers

Passing arguments to an alias

I want to have an alias for the command fold -78 filename | lp How do I set my alias so that the argument passed is filename ?? alias lp='fold -78 | lp' then lp filename wont work cuase this is fold -78 | lp filename (1 Reply)
Discussion started by: pmcg
1 Replies
Login or Register to Ask a Question