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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
05-18-2012, 01:48 AM
|
#1
|
Member
Registered: May 2012
Posts: 39
Rep:
|
how to use a variable for a terminal command
how can i use a variable( user input ) in command line for a command
eg: variable= eth0
ifconfig variable
|
|
|
05-18-2012, 01:52 AM
|
#2
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,417
|
Basically, use a leading '$' when reading a var, no leading $ when setting it eg
Code:
# NB: there must be NO whitespace around '=' in the assignment statement
var=value
echo $var
Try this good tutorial http://rute.2038bug.com/index.html.gz
|
|
|
05-18-2012, 01:54 AM
|
#3
|
Member
Registered: May 2012
Posts: 39
Original Poster
Rep:
|
bu can the "var" be used with ifconfig?
|
|
|
05-18-2012, 02:47 AM
|
#4
|
LQ Addict
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464
Rep:
|
Of course. The shell will get the variable's value and pass it to the command.
|
|
|
05-18-2012, 03:55 AM
|
#5
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Quote:
Originally Posted by omega341991
bu can the "var" be used with ifconfig?
|
Yes. As Nylex said. This is the way the shell acts: it splits a command line into words and if it encounters special characters the shell makes the proper substitutions. After the command line has been somehow rebuilt, it is executed. For example, if you type:
the shell splits this line into two words. The first word must be a built-in or an external command, which will be searched inside the directories listed in the PATH environment variable (unless you use the absolute path). The second word contains a $ followed by a valid token for variable names, the shell searches in memory the value of that variable and substitute it. The resulting command is:
and this one is the actual statement that will be executed. More details about shell substitutions (also called expansions), here.
|
|
|
05-18-2012, 03:57 AM
|
#6
|
Member
Registered: May 2012
Posts: 39
Original Poster
Rep:
|
Its not working in my python code
import os
import subprocess
os.system("var=wlan0")
subprocess.call("timeout 2 tcpdump -XX -nnvvv -p -e -S -U -f -i $var > logt.txt 2>&1", shell=True)
its not working and the result shown is :
tcpdump: option requires an argument -- 'i'
tcpdump version 4.2.1
libpcap version 1.1.1
Usage: tcpdump [-aAbdDefhHIKlLnNOpqRStuUvxX] [ -B size ] [ -c count ]
[ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]
[ -i interface ] [ -M secret ]
[ -r file ] [ -s snaplen ] [ -T type ] [ -w file ]
[ -W filecount ] [ -y datalinktype ] [ -z command ]
[ -Z user ] [ expression ]
|
|
|
05-18-2012, 04:01 AM
|
#7
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,308
|
Of course, they were talking about shells, not python, you have forgotten to mention it.
in python you need to construct the command before executing that call (see string operations)
|
|
|
05-18-2012, 04:03 AM
|
#8
|
Member
Registered: May 2012
Posts: 39
Original Poster
Rep:
|
Sorry about that. So how do i make the code execute?
|
|
|
05-18-2012, 04:42 AM
|
#9
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,308
|
Code:
var = "wlan0"
command = " ".join ( ['timeout 2 tcpdump -XX -nnvvv -p -e -S -U -f -i', var, '> logt.txt 2>&1')
subprocess.call(command)
...
probably works, but I have not tested
|
|
|
05-18-2012, 05:05 AM
|
#10
|
Member
Registered: May 2012
Posts: 39
Original Poster
Rep:
|
stil doesnt work
the error shown is :
command = " ".join ( 'timeout 2 tcpdump -XX -nnvvv -p -e -S -U -f -i', var, '> logt.txt 2>&1')
TypeError: join() takes exactly one argument (3 given)
|
|
|
05-18-2012, 05:07 AM
|
#11
|
Member
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87
Rep:
|
Put a $ before var and remove the comma?
command = " ".join ( 'timeout 2 tcpdump -XX -nnvvv -p -e -S -U -f -i', $var'> logt.txt 2>&1')
|
|
|
05-18-2012, 05:11 AM
|
#12
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,308
|
no, do not put $ sign, I have made a typo:
command = " ".join ( ['timeout 2 tcpdump -XX -nnvvv -p -e -S -U -f -i', var, '> logt.txt 2>&1' ] )
|
|
|
05-18-2012, 06:14 AM
|
#13
|
Member
Registered: May 2012
Posts: 39
Original Poster
Rep:
|
still doesnt work its a bigger error now
File "test.py", line 18, in <module>
subprocess.call(command)
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
|
|
|
05-18-2012, 06:50 AM
|
#14
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,308
|
maybe this is missing from the end of the line:
subprocess.call(command, shell=True)
also you can try to use timeout with full path
|
|
1 members found this post helpful.
|
05-18-2012, 07:40 AM
|
#15
|
Member
Registered: May 2012
Posts: 39
Original Poster
Rep:
|
its works now
thanks a lot pan64
|
|
|
All times are GMT -5. The time now is 05:03 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|