about Unix Shell Script - shell

Can someone help me with this:
How to write a UNIX shell script that takes a parameter. The parameter passed should be the name of a file that is executable. Validate the parameter and if it is not valid output an appropriate error message and ensure your script exits with an overall status of error. If the parameter is valid execute the parameter. If the execution of the parameter fails then output an appropriate error message and ensure your script exits with an overall status of error. If the parameter is valid and executed successfully my script should exit with an overall status of success.

Okay, that's not a prob.
Split your task into 4 simple steps:
How to pass command line arguments to bash script.
How to check whether executable file exists.
How to display message in bash.
How to execute something from bash.
You'r welcome!

Related

What does each field in a typical sh error message mean? Ex. "sh: 1: ipconfig: not found"

I have been receiving Shell error messages, typically due to a bad command, etc., but have been unable to debug the cause of these messages due to not understanding the information being provided.
I have been looking for documentation, but have been unable to find any regarding the format of an sh error message.
Example:
If I use the following command, it will fail, due to no 'ipconfig' command being available:
$ sh -c "ipconfig"
sh: 1: ipconfig: not found
What I'd like to understand is what each 'field' in that message pertains to? I assume it is:
[interpreter]: [???]: [command]: [error related to command]
I can't for the life of me determine what the number refers to, and I can't be sure if my understanding of the other fields is accurate.
Context:
I am debugging a Python2.7 pytest script used for automation testing, and there are numerous points where this script is executing shell commands. However, the output I receive is:
(32512, 'sh: 2: 2: not found')
I know that function being used to execute the shell script returns a tuple with status code and output. I know that that status code is essentially 'command not found', and the error message is also stating that. Another function is returning a string which is used for this command, and I assume what is happening is that somewhere along the way, a bad argument must have been passed and now the script is attempting to execute, what would basically be sh -c "2". I can't be sure though, as these are a lot of assumptions I'm making from a limited understanding of this error message.
If anyone could please enlighten me as to what the fields in this error ACTUALLY mean I'd be forever grateful!!

how to tell if my C program was invoked via shebang?

I've built a little command interpreter (in C++) which can be invoked either directly, or in a script via shebang (#!). It can take arguments on the command line (which appear as argc/argv in my code).
Trouble is, when invoked via shebang, the script itself gets passed to my program as argument 1. That's problematic; I don't want my command interpreter trying to process the script that it was invoked from. But I can't see any easy way to tell when this is the case.
EDIT: As an example, if I have a script called "test" which starts with #!/usr/local/bin/miniscript, and then invoke it as ./test --help -c -foo, I get 5 arguments in my C code: /usr/local/bin/miniscript, ./test, --help, -c, and -foo. If I invoke it directly, then I get four arguments: /usr/local/bin/miniscript, --help, -c, and -foo
How can I tell when my program was invoked via a shebang, or otherwise know to skip the argument that represents the script it was invoked by?
My question was based on a wrong assumption. I believed that two things were happening when a program was invoked via shebang:
Path to that program was passed as the first argument.
Contents of that program were piped to stdin.
So I was essentially worried about processing the content twice. But only item 1 is true; item 2 does not happen (as pointed out by helpful commenters on my question). So if the C code accepts the name of a file to process as a first argument, and ignores any initial line starting with a shebang, then all is right with the world.

How to get the individual exit codes of all the shell scripts invoked by a parent shell script?

I am new to shell scripting and I am trying to invoke several shell scripts from a single parent shell script. Is there a way for me to get the exit codes of all the individual shell scripts(trying to know if they were successful or there was an error and may be save the exit codes to a file, if that's possible) after all the scripts have completed execution?
Any help would be appreciated. Thank you.
After each script finishes you can check the $? variable for its status. But, this only reports the exit status of the last command run, so you need to check it after each script and then (perhaps) set other varaibles to reference when the entire series is finished.

ControlM job calling unix script fails repeatatively

I have a controlM job which calls a shell script that takes 4 command line parameters. The command is below:
sh /appl/Script/Script1.sh ABC /appl/Landing SV_SID_NormalisedEvent_* Y
The 3rd parameter (SV_SID_NormalisedEvent_*) in the command line is a file wildcard/pattern for which the script looks for in the path provided in the second parameter (/appl/Landing).
This job was running fine to date when it aborted for one specific corrupt file: SV_SID_NormalisedEvent_20150810_151805.csv.gz. We have handled this failure manually by ignoring this file and forced ok job
Since then whenever this job is triggered during daily runs it always fires command as below and fails. Somehow the 3rd parameter is always passed as a specific file rather than the wildcard:
sh /appl/Script/Script1.sh ABC /appl/Landing SV_SID_NormalisedEvent_20150810_151805.csv SV_SID_NormalisedEvent_20150810_151805.csv.gz Y
The correct command output when the job was running fine is as below:
sh /appl/Script/Script1.sh ABC /appl/Landing 'SV_SID_NormalisedEvent*' Y
Any pointers to this issue? The above command output is from sysout file created during each run.
We have handled this failure manually by ignoring this file and forced
ok job
It sounds like perhaps the file has not been cleaned up and is being found every time. Is that possible?
The job will run with the command line that is defined in if this still contains the proper parameter SV_SID_NormalisedEvent_*.
I can't think of any other reason you are seeing this behaviour.

Syntax Error when executing shell script with java

I have a shell script which I want to execute within a Java class. The shell script takes an input file and produces an output file from it.
To execute it, I use apache commons exec. Here's my little code:
CommandLine cmdLine = CommandLine.parse("/bin/sh ./generate_void_description.sh ./n3file ./voidfile");
int exitValue = executor.execute(cmdLine);
As a Result i get this Error Message :
./generate_void_description.sh: 44: ./generate_void_description.sh: Syntax error: "(" unexpected
org.apache.commons.exec.ExecuteException: Process exited with an error: 2 (Exit value: 2)
Of course I can read and I understand that the message means that there's a syntax Error in the script but.
1) I didnt write the script and I'm not familiar with bash script syntax
2) The script works perfectly without any errors when I run it on a terminal within ubuntu
Has it something to do with User rights ? I need to create an output file maybe I don't have the permissions to do this if if
The script uses bash features, most noticeably an array in line 44. You'll have to execute the script using /bin/bash, not /bin/sh.
Seems like the bash script is exiting with a return code of 2. If that's normal, try this before making the execute call.
executor.setExitValue(2);

Resources