Why My Arguments Are Being Blocked When Running a Shell Command

bash-if block not working

Bash is very sensitive about whitespace. This should work:

if [ "%s" = "" ]; then

Note that = is used for string comparison and -eq is used for integers.

edit:

More precisely, bash splits your original code like this:

if[%s # supposedly a command
-eq # parameter
=""] # parameter
; # end of command
then # keyword

At this point, Bash realizes that there is an unmatchen then keyword, and doesn't even try to run if[%s (which would fail too).

Check existence of input argument in a Bash shell script

It is:

if [ $# -eq 0 ]
then
echo "No arguments supplied"
fi

The $# variable will tell you the number of input arguments the script was passed.

Or you can check if an argument is an empty string or not like:

if [ -z "$1" ]
then
echo "No argument supplied"
fi

The -z switch will test if the expansion of "$1" is a null string or not. If it is a null string then the body is executed.

How to pass command line arguments to script instead of shell

For well-behaved programs, one possible answer would be this:

#!/usr/bin/env -S interpreter --

The interpreter, whatever it is, would treat the -- as the last option argument. Then it would treat the following argument as the script name, and the remaining arguments as arguments to the script.

Alas, wish doesn't obey this convention. While it supports --, that argument means "this is the last argument that wish itself processes; everything else goes to the script". The argument after -- is not treated a script file to read.

The documentation for -- says that the remaining arguments are passed to the script, but I can't see any way of specifying what that script is, if the -- option is used, other than placing it before the --.

Your best bet might be a shell wrapper:

#!/bin/sh
myname=$0
tkscript="${myname%.sh}.tcl"
exec wish "$tkscript" -- "$@" # Thanks to Donal K. Fellows for exec reminder.

The idea is that you have the above script under the name, say, foo.sh. In the same directory as foo.sh, there is a foo.tcl script: the real one.

The idea is that if the above script is invoked as /path/to/foo.sh, it will calculate the adjacent script's name as /path/to/foo.tcl. That is passed as the argument to wish, then the -- option to say "this is the last argument processed by wish" and then the script's own arguments, which are no longer interpreted by wish even if they look like wish options.

You might not want the .sh suffix on it, but just call it by an unsuffixed name like foo, in which case the tkscript assignment simplifies to:

tkscript="${myname}.tcl"


Related Topics



Leave a reply



Submit