This question already has answers here:
What does the '`' character do in Perl?
(3 answers)
Closed 8 years ago.
I am a beginner in perl.
Just wanted to understand the following code.
sub get_files {
foreach my $customer (keys %customers){
lg("Getting files from ftp for customer $customer", "LOG");
my $ftp_server = $customers{$customer}{'FtpServer'};
my $ftp_user = $customers{$customer}{'FtpUser'};
my $ftp_pass = $customers{$customer}{'FtpPass'};
my $datadir = $datafiles.$customer."/";
`$get_files $ftp_server $ftp_user $ftp_pass $datadir`;
}
}
What does last line in the above subroutine tells?
It invokes the command that's in the string $get_files, passing the command the remaining strings as parameters.
Usually it's used if you want to capture the resulting output and store it in a variable. In this case where the result is being discarded it would be more usual to use system instead:
system $get_files, $ftp_server, $ftp_user, $ftp_pass, $datadir;
although if the command does then produce any output it'll appear on-screen instead of being absorbed by the back-ticks operator.
Related
This question already has answers here:
Access PSObject property indirectly with variable
(3 answers)
Set Value of Nested Object Property by Name in PowerShell
(3 answers)
Closed 2 months ago.
I am traversing an object and would like to replace several path parts with a string stored in another variable. For example:
# a is a complex ps object, the path is valid
$a.b.c
myvalue
$replace = "b.c"
$a.$($replace)
# here I expect 'myvalue', however nothing is returned, as I would like to execute the equivalent of $a.b.c
Is there some way to do this replacement?
This question already has answers here:
Are these alternatives to Invoke-Expression really any safer? And why?
(1 answer)
Powershell reevaluate string to array
(1 answer)
Closed 4 months ago.
I have a variable that stores a command that I would like to execute.
ex:
$C = "echo 'test'"
I have one way to execute this by using the following:
powershell .($C)
however this opens a new instance of powershell. How can I go about executing that code in the same instance? I can't seem to find a way to use dot sourcing without opening that second instance.
EDIT: I can not use IEX
This question already has answers here:
Get only filename from full path of a file
(2 answers)
Closed 5 years ago.
I'm writing a PowerShell script to output the process,
$Process=chknull $_.Properties[0].Value
$Process_new=$Process -replace '(?<! .*) ','_'
The above code outputs the result as entire path of process, Eg:
C:\Windows\System32\Notepad.exe.
Is there a way, where I can get just "Notepad.exe"
If you're getting a full path return like that as a string you can set the value into a variable and then use split on it and grab the last entry in the resulting array.
Example:
("C:\Windows\System32\Notepad.exe." -split "\\")[-1]
Remember to use two "\" as it is also an escape character in regex.
([IO.FileInfo]"C:\Windows\System32\Notepad.exe").Name
This question already has answers here:
PowerShell outputting array items when interpolating within double quotes
(3 answers)
Closed 6 years ago.
Is a function call in a verbatim PowerShell string a thing?
CSharp example:
$"this my { GetSomeValue() }";
I would rather not extract a variable if I don't have to.
I always do it this way:
$verbatimString = 'something' + (GetSomeValue) + 'end'
Since it's verbatim string, you cannot do any trick, to put value directly into it, like you showed in your example.
Remember to call functions without (), you can read about it here: 4988226
This question already has an answer here:
Import Variables from Text File in Powershell
(1 answer)
Closed 6 years ago.
Lets say I have a text file full of variables.
I would like to create a separate file in which I can read and call these variables using powershell.
EX. Variable.txt contains $X=10
Main.ps1 retrieves the variable X from the text file, and doubles it.
Apologize is this is an amateur question, I am very new to powershell.
For your specific purpose, you could have a file, data.ps1 containing your data, like so:
$x=10
Then, a script.ps1 file, similar to the following:
. ./data.ps1
$x = $x * 2
Write-Host "Value X is $x"
Which would give you the following output when you run ./script.ps1:
Value X is 20