LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   replace filename with variable (https://www.linuxquestions.org/questions/programming-9/replace-filename-with-variable-768358/)

Siljrath 11-11-2009 03:29 AM

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.

sycamorex 11-11-2009 03:39 AM

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

chrism01 11-12-2009 12:40 AM

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/


All times are GMT -5. The time now is 08:35 PM.