Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass arguments from csh to program, exactly as they are

Tags:

linux

csh

I have a csh script, which is executed using "source", and passes all its arguments to a program:

% alias foo source foo.csh
% cat foo.csh
./bar $*
# Some uninteresting stuff

If I run source foo.csh a b c, all is OK. But not always:

  1. foo "a b" "c d":
    I expect bar to get two arguments - a b and c d. Instead, it gets 4.

  2. foo a "*" b: The * is expanded to a list of files. I just want the character *.
    Extra credit - foo a * b should work the same way. I know it's more problematic and I'm willing to live without it.

One thing I tried is changing ./bar $* to ./bar "$*". This helps with the asterisk, but now bar always gets everything in a single parameter.

Notes:

  1. Our company uses csh as the login shell, so I must use it when using source. Knowing that csh programming is considered harmful, I implemented all logic in bar and left the bare minimum in the script.

  2. If you suggest redefining the alias, it's important to see that redirection still works (foo | grep hello), and that there's proper cleanup if ctrl-C breaks the script.

like image 253
ugoren Avatar asked Jun 09 '13 09:06

ugoren


People also ask

How do you pass arguments to a shell script?

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.

Can you pass arguments to a bash script?

Passing arguments before runningWe can pass parameters just after the name of the script while running the bash interpreter command. You can pass parameters or arguments to the file.

What does the command #!/ Bin csh do?

When you type in a command at the Unix prompt, you are interacting with the shell. E.g., #!/bin/csh refers to the C-shell, /bin/tcsh the t-shell, /bin/bash the bash shell, etc. You can change your command shell with the chsh command.

Can shell script accept arguments?

You can also pass output of one shell script as an argument to another shell script. Within shell script you can access arguments with numbers like $1 for first argument and $2 for second argument and so on so forth.


1 Answers

Meanwhile, I've found the answer myself:

./bar $argv:q

The :q modifier takes care of things. It passes to bar the exact same parameters foo got.

Source: http://www.staff.tugraz.at/reinfried.o.peter/unix/cshell.html

like image 168
ugoren Avatar answered Nov 16 '22 02:11

ugoren