LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-11-2009, 04:29 AM   #1
Siljrath
Member
 
Registered: Nov 2004
Location: the internet
Distribution: sid, slack, gentoo, witch, bedrock, exherbo
Posts: 162

Rep: Reputation: 43
replace filename with variable


what my script needs:


i want to be able to use a filename in my script several times, and i want to use this script hundreds of times for different files, without having to manually change the filename over and over each time i want it run.

so for example, maybe the script is run with*:
Code:
$ wabob foo.png
and then the script will be able to run all the things i intend to do, without needing "foo.png" being written in the script, presumably replaced with a "variable"? (sorry if thats the wrong terminology, still early days for me and bash)

so what ways are there of achieving this?

(preferably just using bash... but if something else (C?) is required, very well..)



* i dont mind if i just run it with "wabob", and it prompts me for the filename. either way is good i suppose.


ps, sry if this is something painfully simple i've overlooked while reading about bash.
 
Old 11-11-2009, 04:39 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,595
Blog Entries: 1

Rep: Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046Reputation: 1046
You can use the positional variable $1
Whatever you pass as the first argument (can be the name of your file) will be assigned to this variable.
You can access it within the script by eg:
Quote:
ls $1
cp $1 /data/
rm $1
etc.

HTH
 
Old 11-12-2009, 01:40 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
You'll find these links educational
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
  


Reply

Tags
bash, script


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
Read filename as a variable kicap Programming 3 08-12-2009 05:27 PM
Storing part of a filename as a variable VTGuitarMan Programming 5 03-12-2008 07:47 AM
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 02:27 PM
Replace variable with user defined variable ce124 Programming 10 04-13-2007 10:29 AM
how to use environment variable as part of filename beeblequix Linux - Newbie 3 07-11-2006 05:55 PM


All times are GMT -5. The time now is 11:05 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration