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
Issues While Lightweight Core Data Migration
Can Not Import Tensorflow for Swift in Xcode Playground
Avoid Equatable and Hashable Boilerplate, Swift 4.2
Could Not Find an Overload for '-' That Accepts The Supplied Arguments
Back to Table View from Detail Page Without Reload, Swift
Thread Safety of Method Calls on "Shared" Static Constant Property
Xcode Swift: Could Not Insert New Outlet Connection
Is It a Good Way to Access Instance Variable with Self? If I Use a Lot
iOS - Could Not Open Obj File When Convert Mdlasset to Mdlmesh
How to Move an Object Towards a Direction Without Stopping
How to Run Terminal Command in Swift from Any Directory
Return Object for a Method Inside Completion Block
Pfuser That Created Object Cannot Delete Object Despite Acl Writing Access