How to read a directory as parameter in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read a directory as parameter in shell script?
# 1  
Old 10-10-2012
How to read a directory as parameter in shell script?

Hi All,

I have written a shell script to compile an oracle R12 form and also a procedure is called from it. The procedure registers the form, function and creates a menu entry.

I took directory as a parameter. Code is as below:
Code:
#!/bin/ksh
echo "**********************************************************************"
echo "Uploading Form"
echo "**********************************************************************"
 
#Assign default values
CUSTOM_FORM_PATH_DEFAULT=$PER_TOP/forms/US
 
echo "Enter Application Forms Path"
read CUSTOM_FORM_PATH 
if [[ -z ${CUSTOM_FORM_PATH} ]]; then 
  CUSTOM_FORM_PATH=${CUSTOM_FORM_PATH_DEFAULT} 
  echo "Default value ${CUSTOM_FORM_PATH} is considered"
fi 
 
if [ ! -d $CUSTOM_FORM_PATH ]; then 
  echo "Path not found"
  exit 1
fi
 
.............................................

When i give the complete path as /gl02libs/applvis/apps/apps_st/appl/per/12.0.0/forms/US it works.
But if i give as $PER_TOP/forms/US, it comes to the second if block and says path not found

Please suggest.

Thanks,
Veena

Last edited by Franklin52; 10-10-2012 at 04:27 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 10-10-2012
You may use eval:
Code:
echo "Enter Application Forms Path"
read CUSTOM_FORM_PATH 
if [[ -z ${CUSTOM_FORM_PATH} ]]; then 
  CUSTOM_FORM_PATH=${CUSTOM_FORM_PATH_DEFAULT} 
  echo "Default value ${CUSTOM_FORM_PATH} is considered"
fi 

eval CUSTOM_FORM_PATH=$CUSTOM_FORM_PATH

if [[ ! -d $CUSTOM_FORM_PATH ]]; then 
  echo "Path not found"
  exit 1
fi

But, I would suggest that you take this path as an argument to your script than through read. That way, the shell will do the necessary variable expansion, if needed.

Last edited by elixir_sinari; 10-10-2012 at 04:57 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 10-10-2012
Thanks elixir_sinari. It's working now.
# 4  
Old 10-10-2012
Problem: If a user inputs the parameter `rm -Rf ~/`, eval will execute that.

If you absolutely must cram a user-input string into eval, sanitize it first. Maybe remove all backslashes, backticks, and round brackets first.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Passing shell script parameter value to awk command in side the script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff |... (1 Reply)
Discussion started by: Sarita Behera
1 Replies

2. Post Here to Contact Site Administrators and Moderators

Unable to pass shell script parameter value to awk command in side the same script

Variable I have in my shell script diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk -F'~' ''$2 == "$id"' {print $0}' > $new I could see value of $id is not passing to the awk... (0 Replies)
Discussion started by: Ashunayak
0 Replies

3. Shell Programming and Scripting

Read parameter file for a shell script

Hi All, I need urgent Help from all of you here. Below is my code- ================================================== #!/usr/bin/sh cd $1 cat $2 | tr -ds "$" "" > parameter_file.param export `parameter_file.param` chmod 777 parameter_file.param echo $1 echo $2 cd $prmDirInput... (5 Replies)
Discussion started by: Amit786
5 Replies

4. Shell Programming and Scripting

Script Shell Parameter

Hi I have two shell script, the second script takes as a parameter the output variable of the first script, please how to retrieve the variable from the first script to pass as a parameter to the second script? Script1.sh i=0 i=$(($i + 1)) Script2.sh echo $1 Thank you (0 Replies)
Discussion started by: chercheur111
0 Replies

5. Shell Programming and Scripting

Read parameter file in a shell script to unload a DB2 Table???

Hi , I Have following requirement: DB2 Sql query to pass from a parameter file for example, I would create a parameter file with (SELECT column 1, column 2 FROM Table name) then job would read it and create a file with the contents named table.txt How to write/modify below ksh script to... (10 Replies)
Discussion started by: developer.dwh9
10 Replies

6. Shell Programming and Scripting

simple shell - how to get a parameter typed in a shell script

Hi, I am new to unix and using linux 7.2. I would like to create a script that would make it easyer for me to run my java programms. At the moment I have to type java myJavaprogram I am trying to write a script that will allow me to type something like this "myscript myJavaprogram" or maybe... (4 Replies)
Discussion started by: cmitulescu
4 Replies

7. Shell Programming and Scripting

awk/sed script to read values from parameter files

Hi, I am writing a shell program that executes a lot of Oracle SQL Files on different databases based on the enviroment setting value. I am trying to design a parameter file where i can store the environment values for all the databases in the below format Environment File File Name... (6 Replies)
Discussion started by: rajan_san
6 Replies

8. UNIX for Dummies Questions & Answers

Shell script with input parameter

Can anyone help me how to write a shell script which accepts input parameter. My requirement is as follows: I need to run a shell script with a input parameter, and inside the script i will create a file with this input parameter name. Please help me out to create such a shell script. ... (1 Reply)
Discussion started by: jhmr7
1 Replies

9. Shell Programming and Scripting

Read from file as script parameter

I have a file with userIDs, one per line no spaces. I have a script that accepts userIDs as a parameter and outputs information about them. What I have had to do in the past was to modify each line of the file with userIDs to call the script with the userID and pipe the output to a data file. ... (2 Replies)
Discussion started by: vmaxx
2 Replies

10. Shell Programming and Scripting

parameter file for a shell script

Hi I was able to pass parameters to a shell script from the command line but now, I am trying to make the shell script to get those parameters/values from a file. Please give me ideas how to do this or if you have an example or website that shows how to do this. I tried searches but it... (2 Replies)
Discussion started by: bryan
2 Replies
Login or Register to Ask a Question