CSH: Using -d as argument - arguments

I know that -d can be used to test for a directory. If ( $argv[1] == "-d" ) does not work because of that. However, I need to be able to pass -d as an argument to my script. How can I disable the special property of -d so I can pass it as an argument?
Example:
Using ./script -d to launch my script with the -d argument prints a "If: missing file name" error. Using -h for example works just fine.
Obligatory CShell is harmful, but I have to use it.

Solved it by adding a dummy character before the statement. So If ( X$argv[1] == "X-d" ).

Related

Quoting space in bash script

I have a script which needs to pass a string containing a space.
Basically, I want to collect the error return of calling curl "https://my-api.com" -H'X-API-Key: API key here'
The following works, but I'd really rather avoid using eval. I'm sure there is a cleaner way of writing this, but I can't find it.
CURL="curl -s --fail $URL -H\"X-API-Key:$API_KEY\""
return $(eval "$CURL" >> /dev/null)
This is a duplicate of Dynamically building a command in bash, but here is the fix for your code.
Please take the time to read the answers from the parent question.
# Build the curl command with its arguments in an array
curl_cmd=(curl -s --fail "$URL" -H "X-API-Key:$API_KEY")
# Execute the curl command with its arguments from the curl_cmd array
"${curl_cmd[#]}"

curl command works manually but not in script

I have a curl command in a script, when the script is run the command isn't able to fetch a resource (the command itself works, it's the response that's incorrect), but if I copy and paste the same command into the terminal I get the expected response.
After reading this my script looks like this:
jsess=`awk '/\sJSESSION/ { print "\x27"$6"="$7"\x27" }' cookies.txt`
ARGS=( -k -v -b $jsess $url7)
echo "curl ${ARGS[*]}"
curl "${ARGS[#]}"
and the last echo looks like this:
curl -k -v -b 'JSESSIONID=hexystuff' https://secretstuff.com
The last curl doesn't work, but copy-pasting that echo works. Any ideas what could be wrong? Thanks.
The problem seems in the two single quotes, try this :
jsess="$(awk '/\sJSESSION/ { print $6"="$7 }' cookies.txt)"
ARGS=( -k -v -b "$jsess" "$url7")
echo "curl ${ARGS[*]}"
curl "${ARGS[#]}"
args="-k -v -b"
jsess=$(awk '/\sJSESSION/ { print "\x27"$6"="$7"\x27" }' cookies.txt)
url7="https://secretstuff.com"
curl "${args}" "${jsess}" "${url7}"
The use of arrays is not my personal preference, and I believe the current situation demonstrates why. I believe that as much as possible, every individual item of data should be contained in its' own variable. This makes accessing said variables much simpler, and also greatly increases flexibility. I can choose exactly which pieces of information will go into a given command line.

How can I check that a Bash script argument is a user-defined function?

I need to validate that, when running a bash script via the command line, the user is calling, as their first argument, one of the user defined functions in the same script. I know that there is a built in bash command "type," so I am trying to use that. However, I can't seem to get it to work - even when I run the script in -x mode, what is returned from "type -t $1" is just an empty string.
I have tried $(type -t $1), $(type -t "$1"), if [ $(type -t "$1") = "foo"], etc.
Nothing is ever indicated when I test with a function that it is a function, and nothing is returned when I try to validate. I am very new to Bash scripting, apologies if this question is asked elsewhere, I couldn't find it.

curl command not accepting input from file

I'm trying to run a curl command such as:
curl -Sks -XPOST https://localhost:9999/some/local/service -d 'some text arguments'
The above works fine. However, when I place
'some text arguments'
in a file and call the command as:
curl -Sks -XPOST https://localhost:9999/some/local/service -d `cat file_with_args`
I get many exceptions from the service. Does anyone know why this is?
Thanks!
If the file contains 'some text arguments', then your command is equivalent to this:
curl -Sks -XPOST https://localhost:9999/some/local/service -d \'some text arguments\'
— that is, it's passing 'some, text, and arguments' as three separate arguments to curl.
Instead, you should put just some text arguments in the file (no single-quotes), and run this command:
curl -Sks -XPOST https://localhost:9999/some/local/service -d "`cat file_with_args`"
(wrapping the `cat file_with_args` part in double-quotes so that the resulting some text arguments doesn't get split into separate arguments).
Incidentally, I recommend writing $(...) rather than `...`, because it's more robust in general (though in your specific command it doesn't make a difference).

How to print Shell command literally?

In a shell script, is there a way to print command in this literal form after all variables are replaced with real value. For example, mkdir -p $HOME/src, before this command is executed, print something like mkdir -p /home/user/src. Thanks.
Try set -x argument and check this

Resources