Yes. I know I shouldn't be using it for scripting. Yes. It is rubbish. I am indeed a fool.
Nevertheless.
I'd like to see the commands the script is executing, for debugging purposes. I think you get this effect with set -x or set -v in bash scripts.
So e.g., if the script were
#!/bin/tcsh
echo "Hello"
then when it ran, I'd see
echo "Hello"
Hello
Put:
set echo
and/or
set verbose
at the top of your script. That will print out everything that happens in your script and are equivalent to the bash set -x and set -v commands.
Lets say your script name is tcsh_file
Lets assume this file includes shebang as well.
now run this command on terminal
tcsh -x tcsh_file
this will print every line before executing it. it's basically an interactive mode of execution.
Is this what you needed?
Related
When write #!/bin/bash -x in the first line of a bash file (e.g. test.sh), I expect to see some execution information but get nothing... Moreover, when I use the command of bash -x test.sh to run the bash file, it will show information.
Any help for what is going on?
Thanks a lot!
To set debugging to on within your script you will need to use set -x and so:
#!/bin/bash
set -x
.......
The shebang only applies when you execute the file directly:
myfile
./myfile
/path/to/myfile
If you instead execute an interpreter, that interpreter will be used instead, whether or not it's correct and compatible for the script. The best practice is therefore to avoid either of:
bash myfile
sh myfile
How can I print executed commands in fish shell?
I've tried solutions from In a shell script: echo shell commands as they are executed, but they are not compatible with fish shell.
Starting from fish-3.1.0, $fish_trace can be set to enable output similar to Bash’s set -x.
For example,
set fish_trace 1
before commands that should be traced.
Unfortunately fish doesn't yet have an analog of set -x to print commands. This is the issue requesting it. If you have ideas for what the syntax and output should be, please share them :)
The best answer today is, if you are trying to debug a problem you can invoke fish as fish -d 3 and it will show some debugging output as it runs.
I came across $- and found that it is used to determine the flags set for the shell.So I just did an echo $- and it gave me ism . Now i is for interactive shell, m is for monitor mode but I dont know what s means.
Moreover I have sample script test.sh as below
#!/bin/ksh
echo "Hi I am shell and I am about to figure out the flags set for me :-)"
echo $-
When I execute the script like this-- ./test.sh I am getting the flag as h but upon executing sh -x test.sh
I am getting xh . I think this x is coming from sh -x but I am not sure how and why.
I tried to google reagrding the flags but found nothing (maybe because my seach keyword is not proper).
Any information on this will be helpful.Thanks in advance :-)
You are using Korn shell in your script, are you using that interactively as well?
man ksh
is your friend.
- Options supplied to the shell on invocation or by the set
command.
Search for Invocation, the correct term for these settings is options (not flags).
So yes, the x is from the set -x. This is a shortcut for set -o xtrace. To see all those options, and their current settings, set -o (note that only a few have single character shortcuts). Try
set --man
on the command-line (if you have a very old version of ksh, that won't work).
-s just means that commands come from stdin.
I am writing a training tool, it is written in bash to teach bash/unix.
I want a script to run to set things up, then to hand control to the user.
I want it to be easily runnable by typing ./script-name
How do I do this?
I.E.
User types: tutorial/run
The run-tutorial script sets things up.
The user is presented with a task. (this bit works)
The command prompt is returned, with the shell still configured.
Currently it will work if I type . tutorial/bashrc
There are several options:
You start script in the same shell, using source or .;
You start a new shell but with your script as a initialization script:
The first is obvious; I write a little bit more details about the second.
For that, you use --init-file option:
bash --init-file my-init-script
You can even use this option in the shebang line:
#!/bin/bash --init-file
And then you start you script as always:
./script-name
Example:
$ cat ./script-name
#!/bin/bash --init-file
echo Setting session up
PS1='.\$ '
A=10
$ ./script-name
Setting session up
.$ echo $A
10
.$ exit
$ echo $A
$
As you can see, the script has made the environment for the user and then has given him the prompt.
Try making it an alias in your ~/.bashrc file. Add this to the bottom of ~/.bashrc:
alias tutorial='. tutorial/bashrc'
Then close and re-open your terminal, or type . ~/.bashrc to re-source it.
To use this alias, simply call tutorial, and that will automatically get replaced with its alias, as though you had called . tutorial/bashrc.
i have unix shell script which is need to be run like below
test_sh XYZ=KLMN
the content of the script is
#!/bin/ksh
echo $XYZ
for using the value of XYZ i have do set -k before i run the script.
is there a way where i can do this without doint set -k before running the script. or is there something that i can do in the script where i can use value of the parameter given while running the script in the below way
test_sh XYZ=KLMN
i am using ksh.
Any help is appreciated.
How about running this?
XYZ=KLMN ./test_sh //running from directory where test_sh is
If your script needs no other arguments, a quick and dirty way do to it is to put
eval "$#"
at the start of your script. This will evaluate the command line arguments as shell commands. If those commands are to assign a shell/environment variable, then that's what it will do.
It's quick-and-dirty since anything could be put on the command line, causing problems from a syntax error to a bad security hole (if the script is trusted).
I'm not sure if "$#" means the same in ksh as it does in bash - using just $* (without quotes) would work too, but is even dirtier.
It looks like you are trying to use the environment variable "INSTANCE" in your script.
For that, the environment variable must be set in advance of executing your script. Using the "set" command sets exportable environment variables. Incidentally, my version of ksh dates from 1993 and the "-k" option was obsolete back then.
To set an environment variable so that it is exported into spawned shells, simply use the "export" command like so:
export INSTANCE='whatever you want to put here'
If you want to use a positional parameter for your script -- that is have the "KLMN" value accessed within your script, and assuming it is the first parameter, then you do the following in your script:
#!/bin/ksh
echo $1
You can also assign the positional parameter to a local variable for later use in your script like so:
#!/bin/ksh
param_one=$1
echo $param_one
You can call this with:
test_sh KLMN
Note that the spacing in the assignment is important -- do not use spaces.
I am tring this option
#!/bin/ksh
echo $1
awk '{FS="=";print $2}' $1
and on the command line
test_sh INSTANCE=LSN_MUM
but awk is failing.is there any problem over here?
Probably #!/bin/ksh -k will work (untested).