Trying to update the value of global variable in Shell Script - shell

myfunc3()
{
echo $1
}
myfunc2()
{
input=$(myfunc3 10)
echo "someting that will be used later"
}
myfunc1() {
var=$(myfunc2) #this does not update the value
# myfunc2 This updates the value
}
input="$(myfunc3 1)"
echo "The new value is: $input"
myfunc1
echo "The new value is: $input"
I have 3 functions that are interrelated. I am trying to update the value of the input variable. I am able to do it when I simply call the function myfunc2 but when I am trying to get the returned value like var=$(myfunc2) as I want to use it for further uses, then the value of the input variable is not updating.
I am very new to shell scripting and I'm not able to understand the reasoning behind it.
Is there any other way to return the value or calling the function?
Thanks in advance

Related

Get a value and than use that value to compare

I am facing a problem with a PowerShell variable.
My scenario is,
Inside a function, I declare a variable $a, than in a switch, I get a value and store this to variable $a.
Now in another switch in that function, I want to compare $a. But there $a returns null.
Sample code is given below:
function fun
{
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, Position = 0)]
[ValidateNotNullOrEmpty()]
$param
)
$Get_OldData = " " #declare variable
switch ($param){
'param_001' {
$Get_OldData = "test data returned"
}
Default {
$Get_OldData = "test data returned"
}
}
switch ($param){
'param_001' {
$New_Data = "New Data"
#problem is here, can not compare $Get-OldData returns null here
#though data is assigned
if ( $New_Data -eq $Get_OldData){
#logic goes here
}
}
Default {
$New_Data = "New Data"
}
}
}
What is the solution of this problem?
You have multiple issues with your code.
The main issue probably is that you are using $param within your switch which has not been set. Same applies to $Fetch. Another Issue is that your $New-Data variable contains a hypen which you either should replace with an underscore or surround with curly brackets like ${New-Data}.
Also, // does not introduce a comment, you have to use a hash #.

Windows Form can't access PowerShell variable

I'm trying to display a PowerShell variable in a Windows form. I have a button and a label on the form. I can display the string return by TestFunction on the label but not on the Form pop up window.
This is my PowerShell script:
function TestFunction()
{
return "PASSED"
}
Function Button_Click()
{
$testResults = TestFunction
$ResultLabel1.Text = $testResults ## this works - I can see "PASSED"
$TroubleButton1.Add_Click(
{
# This does not work
[System.Windows.Forms.MessageBox]::Show($testResults)
}
)
}
## Call function
Button_Click
Why does the form think $testResults is NULL?
You assign $testResults variable in Button_Click scope, thus you will lose that variable, when you leave that scope. You should save the variable in a scope, which will exists when you press $TroubleButton1 button. Or you can pick value from $ResultLabel1 label instead of variable:
[System.Windows.Forms.MessageBox]::Show($ResultLabel1.Text)

How Do I make Date a Variable?

Right now i am trying to build a script that when run will get the date and store it as a variable so down the line it can be compared with another date value to see if they match
My question is how do I do that and in what forms can you call the date command. This is a script in bash fyi, this is what I am currently trying to do
#Specific Set Variables
SpecficDate=timestamp() {
date +"%T %D"
}
SpecficStatus="Pass" #SetVariable
echo $SpecficStatus
echo $SpecficDate
Any help would be great, thank you
Using your code something like this is what you could use
SpecficStatus="Pass" #SetVariable
timestamp() { date +"%T %D"; }
SpecficDate=$(timestamp)
echo $SpecficStatus
echo $SpecficDate

Storing a function inside a bash array element

How can I define a function as an element of an array?
I know I can do this:
function array[5](){ echo "you are inside the function"; }
And it is a valid function, in the sense that, the instruction array[5], correctly execute the function.
Unlucky, the function is not listed in the keys of the array, so, echo ${!array[*]} does not return 5 as a key.
For instance, the following code:
array[0]="first"
function array[5](){ echo "you are inside the function"; }
array[7]="seventh"
echo ${!array[*]}
Only returns 0 7.
So, how can I add a function to an array element, so that I can loop over it?
You can't really put the function itself in the array. But what you can do is put the function name in that field. Look:
a_pretty_function() {
echo "you're inside a function";
}
array[5]=a_pretty_function
# Execute it:
"${array[5]}"
When you're doing
array[5]() { echo "you are inside the function"; }
you're defining a function called array[5] as you can check with declare -pf array[5].
With this mechanism you can do something horribly ugly:
array[5]() { echo "you are inside the function"; }
i=5
"array[$i]"
and this will execute the function array[5]. I wouldn't recommend such a practice in any case.

In bash -- storing method return value

I'm trying to store a value returned from a method like this: var=$(methodName), but the program never enters the method... It's weird because I do the same thing a few lines earlier (alreadyExists-variable in code sample), and it works fine. I had to do this: var='methodName' to make the program enter the method.
It works, so why care? I'm probably making a mistake, and I need to know what it is and learn from it. Let me know if you need more info to answer the question. Thanks!
overwriteOrNot()
{
echo DEBUG
# This debug string does not print if method is called from "local overwrite=$(overwriteOrNot)"
# but prints if method is called from "local overwrite='overwriteOrNot'"
...
}
local alreadyExists=$(studentNumberExists studentNumber)
if $alreadyExists ; then
# local overwrite=$(overwriteOrNot)
local overwrite='overwriteOrNot'
...
If you're using return, then you need to either directly branch on its result:
if overwriteOrNot; then
: "the function returned 0"
else
: "the function returned something other than 0"
fi
...or store the value of $? immediately after running the function:
overwriteOrNot
local overwrite=$?
Note that return can only return a single-byte integer. If you need to pass content which doesn't fit that type, it needs to be either passed on stdout or in a global variable.
The following:
local overwrite='overwriteOrNot'
assigns a string; it doesn't invoke a function. Instead:
local overwrite=$(overwriteOrNot)
You can check the return value from calling overwriteOrNot with the $? variable, or by checking its numeric return value directly in a conditional statement like:
if overwriteOrNot; then
:
fi
If you assign to overwrite, you can also check its value with any valid test condition such as equality, regular expression match, or emptiness. For example:
if [[ "$overwrite" == "foo" ]]; then
:
fi

Resources