Not able to set a variable to a path - sh

am new in writing shell script. When I execute the following program every time I get "$userInput1=/home/xyx/dest1: No such file or directory"
fun1()
{
.
.
$2=$userInputPath'/*' #$userInputPath holds a path like /home/xyx/dest1
.
.
}
Am using Bourne shell and calling the function like the following manner -
fun1 $input1 userInput1
and after calling the function if I echo $userInput1 it prints nothing. It is to be mentioned that the path /home/xyx/dest1 is valid.

Well normally you would use declare but it is inside a
function. So if you want access to userInput1 outside
of the function you will need to do
fun1 () {
read $2 <<< "$userInputPath/*"
}

Related

Is it possible to wrap an existing function with another function using the same name in fish?

I'd like to create a cd function which expands on the current cd function which (on my machine) lives at /usr/local/Cellar/fish/2.6.0/share/fish/functions/cd.fish. Is this possible?
So something like this:
function cd
if i_can_handle_it
do_my_thing_with $argv
else
call /usr/local/Cellar/fish/2.6.0/share/fish/functions/cd.fish $argv
end
end
You can't have two functions with the same name. You can have a builtin, function, and external command with the same name. The standard solution to this problem is to first rename the function you want to wrap: functions -c cd cd2. Then define your cd function that calls cd2. Note that this works regardless of whether cd has already been run and thus defined because changing the name first causes it to be autoloaded.

How to call a function within & in powershell?

Suppose I have a function in test.ps1, its name is show the code is like below:
. "some_path\some_other_script.ps1"
function show {
write-host "Hello World"
#some other function call from some_other_script.ps1
...
}
How can I call show in follow format (in a & - call operator)
powershell.exe "& '%test_script_path%\test.ps1'\show"
I think I need to dot source test.ps1 first in order to get dependencies in show function from some_other_script.ps1
I know I can create a new script and put the code in show in the new script instead of in a function. In that way, when do &... the script will be invoked. But I don't want to create a new script just for a very simple function
Thanks for any suggestion
You could dot source it and then call it like this.
powershell -Command ". '%test_script_path%\toolsql.ps1'; show"
The ; is used as a separator between commands.
I am really appreciated for the help and time from #Patrick Meinecke, the following command works.
powershell -command ". .\test.ps1;show"
Like he mentioned in the chat,
"So to explain that
the .\ just indicates it's in the current directory
it still needed the other . to tell powershell to load the script into the current context"

Is it possible to check if a given Matlab script is run by itself or called by another script?

I have a Matlab script A that can either be run by itself or be called by another script. I want to enter an if statement in script A that checks if the script is run by itself or called by another script. How can I check this?
You should check out dbstack.
dbstack displays the line numbers and file names of the function calls that led to the current breakpoint, listed in the order in which they were executed. The display lists the line number of the most recently executed function call (at which the current breakpoint occurred) first, followed by its calling function, which is followed by its calling function, and so on.
And:
In addition to using dbstack while debugging, you can also use dbstack within a MATLAB code file outside the context of debugging. In this case, to get and analyze information about the current file stack. For example, to get the name of the calling file, use dbstack with an output argument within the file being called. For example:
st=dbstack;
The following is stolen from the iscaller function posted on the File Exchange.
function valOut=iscaller(varargin)
stack=dbstack;
%stack(1).name is this function
%stack(2).name is the called function
%stack(3).name is the caller function
if length(stack)>=3
callerFunction=stack(3).name;
else
callerFunction='';
end
if nargin==0
valOut=callerFunction;
elseif iscellstr(varargin)
valOut=ismember(callerFunction,varargin);
else
error('All input arguments must be a string.')
end
end
Credit for this approach goes to Eduard van der Zwan.
You can use the function dbstack - Function call stack.
Let's add this to the beginning of your script file, call it 'dbstack_test.m':
% beginning of script file
callstack = dbstack('-completenames');
if( isstruct( callstack ) && numel( callstack ) >= 1 )
callstack_mostrecent = callstack(end); % first function call is last
current_file = mfilename('fullpath'); % get name of current script file
current_file = [current_file '.m']; % add filename extension '.m'
if( strcmp( callstack_mostrecent.file, current_file ) )
display('Called from itself');
else
display( ['Called from somewhere else: ' callstack_mostrecent.file ] );
end
else
warning 'No function call stack available';
end
Add a second script called 'dbstack_caller_test' to call your script:
run dbstack_test
Now when you run dbstack_test from the console or click the green triangle in your MATLAB editor:
>> dbstack_test
Called from itself
When you call it running from dbstack_caller_test
>> dbstack_caller_test
Called from somewhere else: /home/matthias/MATLAB/dbstack_caller_test.m
When you call it within MATLAB's editor using "run current section" (Ctrl+Return) you get
Warning: No function call stack available
Of course you can modify the code dependent on which level you are required to use from the call stack.
As already mentioned in the documentation: "In addition to using dbstack while debugging, you can also use dbstack within a MATLAB code file outside the context of debugging."

PowerShell: How to source a function like source a file?

In my main script, I will first call an init function to initiate many variables which I expected to be used in the script. One way is to use variables whose name are like $script:var1 which are script level variable. But that's kind of ugly and I'd like to use normal variable name, so I need a mechanism to source a function just like source a file.
When source a file, all the variables in that file are available in the calling script.
Use the same syntax that uses dot-operator, just like for sourcing files:
. My-Function
You can also do it in a scriptblock and dot-source that, but the rules are slightly different. You must have a space after the period to dot-source a function, and you don't with a scriptblock.
Both of these will produce 42
$a=0
function init {$a=42}
. init
$a
$a=0
$init={$a=42}
.$init
$a

Executing a Powershell function (which takes parameters) from Powershell command shell

I have a powershell script which is a function and takes parameters. From with the powershell command shell, how do I execute a function? It seems like it works differently for different users.
Is your script, simply a script or does it contain a function? If it is a script and takes parameters it will look something like this:
-- top of file foo.ps1 --
param($param1, $param2)
<script here>
You invoke that just like a cmdlet excecpt that if you are running from the current dir you have to specify the path to the script like so:
.\foo.ps1 a b
Also note that you pass arguments to scripts (and functions) space separated just like you do with cmdlets.
You mentioned function, so if you script looks like this you have a couple of options:
-- top of file foo.ps1 --
function foo ($param1, $param2) {
<script here>
}
If you run foo.ps1 like above, nothing will happen other than you will define a function called foo in a temporary scope and that scope will go away when the script exits. You could add a line to the bottom of the script that actually calls the foo function. But perhaps you are intending on using this script more as a reusable function library. In that case you probably want to load the functions into the current scope. You can do that with the dot source operator . like so:
C:\PS> . .\foo.ps1
C:\PS> foo a b
Now function foo will be defined at the global level. Note that you could do the same thing within another script which will load that function into the script's scope.
Take a look at this post, maybe it is the right for you.
How to pass command-line arguments to a PowerShell ps1 file
Anyway, the built-in $args variable is an array that holds all the command line arguments.