I'm trying to configure mutt on Fedora 30 with user-defined variable (my_<name>). To avoid repeating the same path name all over the place, I'd like to use a variable to specify the mailboxes location. I create the list of mailboxes using a shell command.
I use the mailboxes parameter to create list of mail boxes dynamically. I'm trying to re-use the $folder variable to tell the shell command the directory to go to find the mailboxes.
### where are my email accounts ?
set my_mail_root_dir = $HOME/Mail
set mbox_type = Maildir
### folders for my Hotmail account
set folder = $my_mail_root_dir/hotmail
set mbox = $folder/Inbox
set spoolfile = $folder/Inbox
### create list of mailboxes trying to use '$folder'
mailboxes `echo -n "+ "; cd $folder; find . -maxdepth 1 -type d -name "*" -printf "+'%f' "`
### it will work using hardcoded directory:
#mailboxes `echo -n "+ "; cd ~/Mail/hotmail; find . -maxdepth 1 -type d -name "*" -printf "+'%f' "`
I'm looking for a way to use an existing variable inside the mailboxes shell command.
Is it something possible ?
Try adding this line to your configuration above the mailboxes command.
setenv folder "$folder"
This should add environment variable folder containing $folder value, so the bash as the neomutt child process will have this variable visible.
Related
I'm trying to set up a local Git server on Windows the way it is described on this website: https://github.com/PowerShell/Win32-OpenSSH/wiki/Setting-up-a-Git-server-on-Windows-using-Git-for-Windows-and-Win32_OpenSSH. When I try to set the variable $machinePath ($machinePath = ${C:\Program Files\Git\mingw64\bin}::GetEnvironmentVariable('Path', 'MACHINE')) I get an error message telling me that accessing the path C:\Program Files\Git\mingw64\bin was denied. I do run PowerShell as Administrator. Can anyone tell me how to fix that?
The first step of your linked guide says to run these commands to add git to your PATH globally. There's no reason to change them unless you installed git somewhere else:
$gitPath = Join-Path -Path $env:ProgramFiles -ChildPath "git\mingw64\bin"
$machinePath = [Environment]::GetEnvironmentVariable('Path', 'MACHINE')
[Environment]::SetEnvironmentVariable('Path', "$gitPath;$machinePath", 'Machine')
Note that setting machine-level environment variables does require running-as-administrator.
As for powershell syntax:
# accessing a class's member method/property is done via
[ClassName]::Method('parameter1','p2')
# curly brackets are usually script blocks or hash tables
$sciptblock = { ping localhost }
$hashtable = #{ key1 = 'value1'; key2 = 'value2'}
# they can also be used to set a variable name with spaces:
${a b c} = 'abc'
# BUT if you have a file path, powershell will GET/SET the data of the file:
${c:\temp\test.txt} = 'test file'
I'm using powershell to manage the building of some .net projects on my machine, and I'd like to create aliases for them. The only trick is that I'd like to only use the aliases when I'm in the folder containing all the code. Is there a way to apply aliases only in a particular folder?
I wouldn't recommend it due to its obscurity, but you could dynamically add and remove aliases or functions via the prompt function that determines the interactive prompt string, because it is called after every command.
Note that PowerShell aliases only allow aliasing command names (or paths); that is, you can't bake arguments into them, which is why the following example uses a function instead (but it would work analogously for aliases):
function prompt {
# Define function `gs` on demand whenever the current location is a in a Git
# repo folder, and remove it when switching to any other folder.
if (Test-Path ./.git) { function global:gs { git status $Args } }
else { Remove-Item -EA Ignore function:global:gs }
# Define the standard PS prompt string.
"PS $PWD$('>' * ($nestedPromptLevel + 1)) "
}
To lessen the obscurity, you could modify the prompt string to signal whether or not the folder-specific commands are in effect:
function prompt {
# Define function `gs` on demand whenever the current location is a in a Git
# repo folder, and remove it when switching to any other folder.
if (Test-Path ./.git) {
$indicator = '[repo]'
function global:gs { git status $Args }
} else {
$indicator = ''
Remove-Item -EA Ignore function:global:gs
}
# Define the standard PS prompt string.
"PS $PWD $indicator$('>' * ($nestedPromptLevel + 1)) "
}
Now your prompt will contain substring [repo] (e.g., PS /Users/jdoe/Projects/foo [repo]>) whenever the current location is a Git repo folder.
Trying to create a powershell script to perform the following tasks:
- Asks for a username to use when creating folder and assigning permissions
- Create folder on our NAS with that username for a name
- Create DFS folder with same name and assign it a target path
- Assign explicit permissions for the user to that DFS folder
My script looks like the following:
$Username = read-host "Type Username"
#Create new folder on the NAS and set necessary permissions
New-Item -Path \\NAS\USER_SHARE\$Username -ItemType Directory
#Create new DFS folder and target
New-DfsnFolder -Path "\\DOMAIN.local\user\$Username" -TargetPath "\\NAS\USER_SHARE\$Username"
#Assign explicit permissions to DFS folder for user
dfsutil property SD grant \\DOMAIN.local\user\$Username DOMAIN\$Username:F protect
It performs everything but the last line. It creates the DFS folder but doesn't give permissions. No error message is given, just prints out the help info for the dfsutil command. When I run the last line independently with a static username, it is successful. So I believe there is something with the syntax of the $Username:F part of that last line of code messing it up.
How can I separate that last section to make this work?
Try the invoke operator: &. Also, your variable call to $Username is going to cause issues with the colon, so you want to use {} around the variable name.
$Params = #('property','SD','grant',
"\\DOMAIN.local\user\$Username",
"DOMAIN\${Username}:F",
'protect')
& dfsutil #Params
I created a log file using powershell in .txt or .config. Its look like :
# Hello
## This is readme.txt file
## This is commented line
# This is powershell command output
#
# File generated for read, re-load and edit the values
## -- many more comment is there
# Users can change values..
## There is no relation between $RegPath and $RegValue, this are only variables.
# this are registry path,
$RegPath = (
"\\hklm\software\microsoft\123",
"\\hklm\software\Adobe\123",
"\\hklm\software\Fax\123",
"\\hklm\software\IE\123");
# this are registry value.
$RegValue = (
"0",
"123",
"abc",
"456asdccxv",
"update",
"serv");
#this are some services with 0/1
# Win 7 OS exist
$IsWin7OS = 1
# Service pack installed
$IsSPInstalled = 0
# Check office
$MSOffice = 1
# This setting name is
$SettingName = "ReadMe.txt"
This is sample ReadMe.txt. I want to read this file in powershell and want to get values of $RegPath, $RegValue, $IsWin7OS, $IsSPInstalled, $MSOffice and $SettingName in powershell platform. Then I will update this value and save again in same file.
DISCLAIMER: This is a security WORST practice. It really is quite dangerous. That said, an easy way to read in this data is:
PS> Invoke-Expression (Get-Content C:\readfile.txt -Raw)
PS> $RegPath[0]
\\hklm\software\microsoft\123
The reason it is bad is that if someone adds this remove-item c:\ -recurse -force to the file the command above will execute that and it will be a bad day for you. What I recommend is that you put the data in a .psd1 file if you can in the form of a hashtable. PowerShell will not execute arbitrary code in that case. Or you could store the data as CLIXML or JSON and then read it back in with Import-Csv or ConvertFrom-Json.
RyanWilcox had posted a script at here, that can use the following command to add subrepository automatically:
$ cd $TOP_OF_HG_PROJECT
$ addsubrepo.sh $URL_TO_HG_PROJECT relative_path/you/want_to/put_the_subrepo
How to translate it into Windows batch or powershell script?
Here is a quick and dirty translation. Haven't tested as I got no Mercury around. The initial script seems to be easy enough to translate into Powershell.
# Project and relative paths as script parameters
param([string]$project, [string]$relPath)
# Let's see if there is an item .hg. If not, report error and quit
if((test-path ".hg") -eq $false ) {
"You MUST run this at the top of your directory structure and use relative paths"
return
}
# Call Mercury
& hg clone $project $relPath
# Add data to .hgsub using composite formatting string
add-content -path ".hgsub" -value $("{0} = {1}" -f $relPath, $project)
# Check that .hgsub exists and issue Mercury commands if it does
if(test-path ".hgsub") {
hg add .hgsub
hg commit
} else {
"failure, see error messages above"
}