[Gutsy]How do I represent a variable that will be a file name in a script?

Nils Kassube kassube at gmx.net
Mon Dec 24 21:36:48 UTC 2007


John Toliver wrote:
> I'm not very good at scripting so I'm sorry if the description isn't as
> clear as it could be but I am trying to write a  nautilus script for
> shredding a file.  I want to be able to right click on the file I wish
> to destroy, select "scripts>shred" and the script then knows to act on
> this file.  How do I represent a variable in Linux scripting?  I think
> it should be the equivalent of "%1" in batchfile scripting on windows.

I don't know if you are referring to a bash script or if nautilus has a 
special scripting language. However, if it is bash, the command line 
parameters are "$1", "$2", "$3", etc. As an extra advice: you should 
quote the filenames because otherwise there might be problems if there 
are spaces in the filenames. E.g. a very simple program to copy files 
would look like this:

#!/bin/bash
cp "$1" "$2"

If it is called "copy" it would be executed on the command line like this:

copy file1 file2

where file1 would be $1 and file2 would be $2.

> Second, I wish I knew how to describe this better but what does the
> terminal recognize as "operators" on files.  In windows if I type:
> "copy <sourcepath>\f*.exe <destinationpath\foldername>"  it will copy
> all files starting with "f" that are executables to the path I
> specified.  what are the equivalents of operators like "*.*", 

In a bash script "*.*" would be files with at least one dot in the name. 
Use "*" alone, if you mean any file. However, usually hidden files are not 
included (hidden files start with a dot). There is no special extension 
for executable files but executable files have special permissions. And 
there is one more important difference to DOS / Windows: The DOS shell 
gives the string "some\path\*.exe" as a parameter to the program. A shell 
script gets a list of files (including path if given on the command line) 
because the shell expands the wildcards already, unless the command is 
quoted on the command line.

> the 
> vertical line "|" that goes before the "more" command in windows
> terminal that splits output to a page at a time, and other such
> commands. 

The vertical line is the same as in DOS / Windows.

> First what are these things called, 

"*" and "?" are wildcards and "|" is a pipe.

> secondly where can I go  
> to read about them?

Try the manual for bash which is available online at 
<http://www.gnu.org/software/bash/manual/bash.html> or if you prefer a 
paper manual, I recommend the O'Reilly book "Learning the bash shell" by 
Cameron Newham & Bill Rosenblatt.


Nils




More information about the ubuntu-users mailing list