Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

TCL-how do I make the ele in $argv into ind var [SOLVED]

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
6 posts • Page 1 of 1
Author
Message
brent_weaver
Guru
Guru
User avatar
Posts: 510
Joined: Thu Jul 01, 2004 7:57 pm
Location: Burlington, VT

TCL-how do I make the ele in $argv into ind var [SOLVED]

  • Quote

Post by brent_weaver » Fri Jul 11, 2008 2:45 pm

I am wrting a script and need to make $argv elements into individual variables that I can later reference. For example:

./tclscript element1 element2

I would like to split them up and set each as a variable to be later used in the script.

Any help is MUCH appreciated!
Last edited by brent_weaver on Tue Jul 15, 2008 1:09 pm, edited 1 time in total.
Brent Weaver
Top
timeBandit
Bodhisattva
Bodhisattva
User avatar
Posts: 2719
Joined: Fri Dec 31, 2004 1:54 am
Location: here, there or in transit

  • Quote

Post by timeBandit » Sat Jul 12, 2008 3:28 am

Use lindex to access individual elements of the argv list.
lindex argv 0 evaluates to the first argument, lindex argv 1 to the second, and so on.

Code: Select all

#!/bin/sh
# Re-execute with Tcl shell:\
exec tclsh "$0" "$@"

for {set i 0} {$i < [llength $argv]} {set i [expr $i+1]} {
        puts [format "argv(%u): %s" $i [lindex $argv $i]]
}

Code: Select all

~
18 :0 $ chmod +x foo.tcl 
~
19 :0 $ ./foo.tcl fee fie foe fum
argv(0): fee
argv(1): fie
argv(2): foe
argv(3): fum
Edit: Changed example to include the magical portable shebang line.
Last edited by timeBandit on Mon Jul 14, 2008 10:09 pm, edited 2 times in total.
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Top
brent_weaver
Guru
Guru
User avatar
Posts: 510
Joined: Thu Jul 01, 2004 7:57 pm
Location: Burlington, VT

  • Quote

Post by brent_weaver » Sun Jul 13, 2008 1:30 pm

Thank you SOOOO MUCH for your time and the GREAT response!!!
Brent Weaver
Top
brent_weaver
Guru
Guru
User avatar
Posts: 510
Joined: Thu Jul 01, 2004 7:57 pm
Location: Burlington, VT

  • Quote

Post by brent_weaver » Mon Jul 14, 2008 3:28 pm

One more question. How would I write this so it just takes two params and sets a var for each.

Example:

./script.tsh p1 p2

and it set $param1 = p1 and $param2 = p2?

I am so used to perl I am having a hard time wrapping my skull around this!

Thanks!
Brent Weaver
Top
bblount
Tux's lil' helper
Tux's lil' helper
Posts: 80
Joined: Mon Nov 08, 2004 2:21 pm

  • Quote

Post by bblount » Mon Jul 14, 2008 4:33 pm

#!/usr/bin/tclsh

set param1 [ lindex $argv 0 ]
set param2 [ lindex $argv 1 ]

puts "$param1|$param2"


If you like you could also add a check to make sure you have exactly 2 args, and if not you could exit with a nonzero return code or something like that.
Top
timeBandit
Bodhisattva
Bodhisattva
User avatar
Posts: 2719
Joined: Fri Dec 31, 2004 1:54 am
Location: here, there or in transit

  • Quote

Post by timeBandit » Mon Jul 14, 2008 10:09 pm

Side note: I edited my example script above to include a little trick that avoids the need to know where tclsh is installed (provided it's on the PATH).
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Top
Post Reply

6 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic