LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-25-2013, 12:33 AM   #1
abhiJ
LQ Newbie
 
Registered: Oct 2013
Posts: 4

Rep: Reputation: Disabled
Exclamation How to pass an argument to a variable that has been assigned a script?


Hello everyone,

i am a newbie to shell scripting..i am trying to pass an argument to a variable which i've assigned a script. My script looks like this :

$ a=`func_unload()
> {
> if [[ $# -eq 0 ]]
> then
> echo "No argument passed"
> exit
> fi
>
> echo $1
> sqlplus -S scott/tiger@abiorcl <<EOF
> set echo off head off feedback off pagesize 0 linesize 1000 space 0
> trimspool on
> spool job_upload_$1.dat
> select
> E.elapsed_time||'|'||SCOTT.DIM_JOB_DEF.job_def <
> from SCOTT.FCT_JOB_INSTANCE,SCOTT.DIM_JOB_DEF where
> to_char(start_datetime,'mon')='$1' and SCOTT.FCT_JOB_INSTANCE.job_def_id =
> SCOTT.DIM_JOB_DEF.job_def_id;
> spool off
> exit;
> EOF
>
> }
> func_unload $1`

$

i tried a few options like :

$ echo $(a jan)

and

$ echo $a $jan

but nothing seems to work. Pls help me out!!!

Last edited by abhiJ; 10-25-2013 at 12:37 AM.
 
Old 10-25-2013, 12:37 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I do not really understand, but probably:
Code:
func_unload {
...
}
a=`func_unload $1`
please tell us what is your goal, what do you want to achieve (how should it work?)
 
Old 10-25-2013, 12:53 AM   #3
abhiJ
LQ Newbie
 
Registered: Oct 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Lightbulb

Quote:
Originally Posted by pan64 View Post
I do not really understand, but probably:
Code:
func_unload {
...
}
a=`func_unload $1`
please tell us what is your goal, what do you want to achieve (how should it work?)
i recently found out that we can run certain commands by assigning them to a variable, for example :

$ abc=`ls |wc -l` will return total no. of files when we do :

$ echo $abc

so tried to assign a whole script to the variable 'a' as posted. this script will accept command line arguments like 'jan' or 'feb' or any name of a month when running via a editor like 'vi'. So i tried to pass arguments to the variable as i would have done when running the script normally. Is there a way to do so?? I hope you understand my point now...!!!

Last edited by abhiJ; 10-25-2013 at 12:54 AM.
 
Old 10-25-2013, 01:06 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
there is a way to do that, but this is not the usual way (I mean assign a script to a variable is quite unusual).
you may try eval $variable to achieve something like that, but it is not really recommended and possibly unsafe.
 
Old 10-25-2013, 01:10 AM   #5
abhiJ
LQ Newbie
 
Registered: Oct 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by pan64 View Post
there is a way to do that, but this is not the usual way (I mean assign a script to a variable is quite unusual).
you may try eval $variable to achieve something like that, but it is not really recommended and possibly unsafe.
Doesn't work!
 
Old 10-25-2013, 01:26 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I have no idea what have you really done, so I cannot help you further....
 
Old 10-25-2013, 01:29 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@abhiJ: You mention in your first post that you created a script, but if I look at this "script" it seems that this is not the case, it is a function run from the shell.

You also mention this:
Quote:
this script will accept command line arguments like 'jan' or 'feb' or any name of a month when running via a editor like 'vi'
How does vi come into the picture?

It seems that you are trying to do something the hard way and your posts do not make clear what it is you want/need to do.

Am I right in assuming that you want to extract some information from a database, based on the input given (jan, feb, ...) and store all the output generated in a variable?

If so; What is this variable going to be used for?
 
Old 10-25-2013, 02:11 AM   #8
abhiJ
LQ Newbie
 
Registered: Oct 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Question

i am just trying to find out if there is a way to pass an argument (which is used to retrieve certain data from a script) to a variable that has been assigned a script???
 
Old 10-25-2013, 02:21 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Still not much to go on. I'll give you 2 general examples.

1) Using a script: Create the following script and make it executable:
Code:
#!/bin/bash

[[ $# -eq 0 ]] && echo "No argument passed" && exit 1

echo $1

# add sql code here
Now you can assign the output to a variable in the following way:
Code:
a=$(./script.name.sh jan)
echo "$a"
2) Using a function: Create a file with the following content:
Code:
function unload() {
  [[ $# -eq 0 ]] && echo "No argument passed" && return

  echo $1

  # add sql code here
}
Parse the file to make the function available in the shell:
Code:
. file_name
You can now do the following from the shell:
Code:
a=$(unload feb)
echo "$a"
 
Old 10-25-2013, 02:30 AM   #10
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,149

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
check out this simple code below if this is what you want:

Quote:
$x = "String x"

echo "My variable is: $x"

Output:
My variable is: String x


$y = "String y"

echo "This is variable x and y: $x $y"

Output: This is variable x and y: String x String y
check if variable is empty or not:

Quote:
xvar="xx"
if [ -n "$xvar" ]; then
echo "not empty"
else
echo "empty"
fi

output is: not empty
but if you remove "xx" from xvar, of course the output is "empty"
Good luck!!!

Last edited by JJJCR; 10-25-2013 at 02:35 AM. Reason: edit
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I pass wildcard as an argument to a shell script? Jykke Programming 8 03-09-2013 10:33 AM
[SOLVED] pass a file name with space as single argument to script sumeet inani Linux - Newbie 5 08-26-2012 04:45 AM
How do I pass complicated commands (With bar's) as an argument to another script? theaceoffire Programming 8 04-22-2010 09:40 AM
Howto pass an argument to alias of a script? Goose1 Programming 1 02-17-2008 06:14 PM
PHP pass argument to shell script monzter Programming 2 08-14-2004 06:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration