LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-18-2012, 01:48 AM   #1
omega341991
Member
 
Registered: May 2012
Posts: 39

Rep: Reputation: Disabled
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
 
Old 05-18-2012, 01:52 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,417

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
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
 
Old 05-18-2012, 01:54 AM   #3
omega341991
Member
 
Registered: May 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
bu can the "var" be used with ifconfig?
 
Old 05-18-2012, 02:47 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Of course. The shell will get the variable's value and pass it to the command.
 
Old 05-18-2012, 03:55 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by omega341991 View Post
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:
Code:
ifconfig $variable
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:
Code:
ifconfig eth0
and this one is the actual statement that will be executed. More details about shell substitutions (also called expansions), here.
 
Old 05-18-2012, 03:57 AM   #6
omega341991
Member
 
Registered: May 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
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 ]
 
Old 05-18-2012, 04:01 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,308

Rep: Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702
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)
 
Old 05-18-2012, 04:03 AM   #8
omega341991
Member
 
Registered: May 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
Sorry about that. So how do i make the code execute?
 
Old 05-18-2012, 04:42 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,308

Rep: Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702
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
 
Old 05-18-2012, 05:05 AM   #10
omega341991
Member
 
Registered: May 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
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)
 
Old 05-18-2012, 05:07 AM   #11
Mark1986
Member
 
Registered: Aug 2008
Location: Netherlands
Distribution: Xubuntu
Posts: 87

Rep: Reputation: 11
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')
 
Old 05-18-2012, 05:11 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,308

Rep: Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702
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' ] )
 
Old 05-18-2012, 06:14 AM   #13
omega341991
Member
 
Registered: May 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-18-2012, 06:50 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,308

Rep: Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702Reputation: 7702
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.
Old 05-18-2012, 07:40 AM   #15
omega341991
Member
 
Registered: May 2012
Posts: 39

Original Poster
Rep: Reputation: Disabled
its works now
thanks a lot pan64
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 resize the terminal window from the command line in the gnome terminal QuIcKSpArK Linux - Newbie 5 04-21-2012 03:04 PM
[SOLVED] Make linux terminal transparent with terminal command? yooy Linux - Newbie 1 05-11-2010 04:24 AM
Is command line invocation of gnome-terminal to run more than one command possible? narnie Programming 4 02-17-2010 11:39 PM
[SOLVED] append command output to file by giving command in terminal sumeet inani Linux - Newbie 4 07-03-2009 11:36 AM
Terminal editor's path/variable Will Linux - General 1 11-10-2001 04:32 AM

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

All times are GMT -5. The time now is 05:03 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