How Do I make Date a Variable? - bash

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

Related

Trying to update the value of global variable in Shell Script

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

If statement in Jenkinsfile, comparing strings not working

I am trying to add a "validation" stage in Jenkinsfile based on the day of the week. If today is Sunday, validation is required, otherwise not.
the if statement is not working
here I am declaring the variable
DAY=sh(returnStdout: true, script: 'date +"%a"').trim()
and here is the stage
stage('validation') {
steps {
script {
if ( DAY == "SUN" ) {
echo "Validation is required, today is $DAY"
}
else {
echo "No validation required, today is $DAY"
}
}
}
}
and here is the output
No validation required, today is Sun
the value of the variable Day is correct, but the if statement doesnt work correctly
thanks in advance
It looks like the comparison is failing because the case of the word in DAY is different.
Try this
if ( DAY == "Sun" ) {
echo "Validation is required, today is $DAY"
}
else {
echo "No validation required, today is $DAY"
}
Another approach, to be sure of what you are comparing, is at least to transform the result in upercase. That way, your test will work unchanged.
And be sure to force an English output for date +"%a"
(on my French setup, I get "dim.", not "Sun")
DAY=sh(returnStdout: true, script: 'LANG=en_us_88591 date +"%a"').trim().toUpperCase().replaceAll('.','')
That way, your Jenkinsfile will work on any workstation, no matter its locale.

Ansible : how to pass "{{ABC}}" as a string

Under my jinja file i ve this line
- mockString= "{{ABC}}"
My mockString must have the exacte value "{{ABC}}" where the first letter is " , the second is { the third is also { ect...
Now when converting my template file ; it seems that i sees "{{ABC}}" as a variable , where it tries to interpret and change by its value , what is not my purpose.
How may i pass it as a simple string
Suggestions ??
Try passing it like this {{'"{{ABC}}"'}} this should work
you can try like this :
mockString= \"{{ACB}}\"

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

Access array variable in session (CodeIgniter)

I have an array called config. I'm trying to echo a variable from the array in the session.
I've tried:
echo $this->session->userdata('config['item']');
but it doesn't work. What's wrong with my syntax here? I've print_r'd my session and the items are in the config array. I've also tried:
echo $this->session->userdata("config['item']");
I get no errors this time, but no data either.
If config is an array . And item is string name of what you want to get from config then
echo $this->session->userdata($config['item']);
or
echo $_SESSION[$config['item']];
If config is an array inside session you should first get it.
$tmp = $this->session->userdata('config');
echo $tmp['item'];
or
echo $_SESSION['config']['item']
Sorry for my english.
If you want to use the session array, use the variable, not the function:
echo $this->session->userdata['user_data']['item'];
If you want to write:
$this->session->userdata['user_data']['item'] = 'value';
$this->session->userdata['other_data']['other'] = 'value2';
$this->session->sess_write();
This allows you to edit values in array just like you do with $_SESION['user_data']['avatar'] = $avatar, with 'only' one extra line and only using CI library.
Always escape your string it should be this way:
echo $this->session->userdata('config[\'item\']');

Resources