Is it possible to create a new environment variable with an alias?
I'm aware that I could set the environment variable as an alias to begin with, but I need it to be dynamic and able to change on the fly.
E.g, I work in Film on different shots at a time, so when I open a program I set the shot through the my environment variable like this:
export shot=abc_123
What I would like to do is not have to type out 'export shot=' and just type 'shot abc_123' in to a shell which would change the env var.
I am using zsh on osx.
Any ideas on how I can integrate this?
We can create a zsh function that implements this in our ~/.zshrc like so:
function setShot() {
export shot="$#"
}
Then, we can call the function, and all params will be set to the shot variable.
Small example:
$
$ which setShot
setShot () {
export shot="$#"
}
$
$ echo $shot
$
$ setShot testShot_1234
$
$ echo $shot
testShot_1234
$
Related
I use the following code to both output something to stdout, and pipe it to a program:
function example() {
local fd1
{
exec {fd1}>&1
{ echo hi >&$fd1 } | true
} always { exec {fd1}>&- }
}
I am wondering if I can safely drop always { exec {fd1}>&- }. fd1 goes out of scope after the function finishes anyways.
You need to keep always { exec {fd1}>&- }. If you get rid of that, the variable containing the file descriptor will go out of scope, but the file descriptor won't be closed, resulting in leaking it. You can see this by doing ls -l /proc/$$/fd before and after running your function without that line. Each run of the function will permanently add another FD to that list. Eventually, you'll run out of file descriptors and won't be able to open any new ones, which will break things.
I can declare a bash variable as read only:
var=myname
declare -r var
and then when I try to change the value :
var=anothername
I get (in zsh for example)
>>read-only variable: var
How can I "undeclare" or unset this variable?
I found this here unset:
$ cat << EOF| sudo gdb
attach $$
call unbind_variable("var")
detach
EOF
You can't in bash, readonly means you can't change that variable in anyway, including getting rid of it.
etc/profile:
if [ "$EUID" = "0" ] || [ "$USER" = "root" ] ; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${ROOTPATH}"
else
PATH="/usr/local/bin:/usr/bin:/bin:${PATH}"
fi
export PATH
unset ROOTPATH
echo $PATH:
/sbin:/bin:/usr/sbin:/usr/bin
So:
/usr/local/bin is not showing up, so I think it may be using a different file.
I have tried putting export PATH=$PATH:/usr/local/bin and it's fine but that is not permanent.
Check your shell init files, for bash that would be /etc/bashrc or /etc/bash.bashrc or something like that as well as ~/.bashrc and ~/.bash_profile. Most shells have similiar init scripts, check the manual.
Also check out /etc/env.d/* and /etc/environment.
I have a set of scripts that share some environment variables that are used as status flags.
Consider:
./script1.sh; ./script2.sh; # I execute 2 scripts within the same shell.
Now each of these scripts executes the below script at regular intervals that sets (refreshes) the environment variables:
. ./setEnvVariables.sh #This executes it in the context of the current shell
and thus making the environment variables accessible across both scripts.
setEnvVariables.sh contains the variables that I want to use in other scripts executed within the same shell.
Some of these variables act as flags that may be changed manually in the file during the course of the script1.sh,script2.sh,.. scripts execution.
Another approach would be to have the flags saved in a file and create typical get/set functions to read the file and return/set the flag value. These flags are set by me to simplify controlling the scripts functionality.
Is there a better way of handling this? This kinda falls in the getter-setter design pattern...
If you are using ksh93, not ksh88, a more elegant way would be to implement this using discipline functions.
They allow to implement getter et setters functions for shell variables. You might then create a getter that would pick the variable value from a shared storage area (a file, an ldap directory, a database, whatever) and a setter that would update the same back-end.
Here is a quick example:
a.ksh
function shared.get
{
.sh.value=$(</tmp/shared)
}
function shared.set
{
echo ${.sh.value}>/tmp/shared.new
mv /tmp/shared.new /tmp/shared
}
set -x
echo $shared
shared=22
echo $shared
./b.ksh
echo $shared
b.ksh
function shared.get
{
.sh.value=$(</tmp/shared)
}
function shared.set
{
echo ${.sh.value}>/tmp/shared.new
mv /tmp/shared.new /tmp/shared
}
set -x
echo $shared
shared=11
echo $shared
b.ksh running as subshell modifies the value of the shared variable in the parent process (a.ksh).
$ ./a.ksh
+ echo 11
11
+ shared=22
+ echo 22
22
+ ./b.ksh
+ echo 22
22
+ shared=11
+ echo 11
11
+ echo 11
11
I am trying to build a script that use "sed" to do the following :
Search for files ending with .config (ie: test.js.config) in the current directory and all under it.
Replace a string with another one (ie: {TEST} -> 50)
Save the result in a new file that does not have the .config at the end (ex: test.js)
Delete the original .config file (ie: test.js.config)
It would be great if it would be able to use another file that has the "string to search" with a "string to replace", like a dictionnary :
{TEST} = 50
{FIRST_NAME} = Alex
You can put a bunch of sed s/// commands in a file:
$ cat change.sed
s/{TEST}/50/g
s/{FIRST_NAME}/Alex/g
If you have the following config input:
$ cat test.js.config
alert({TEST})
alert({FIRST_NAME})
The result will be:
$ sed -f change.sed test.js.config > test.js
$ cat test.js
alert(50)
alert(Alex)
The syntax is not great, but it is neither horrendous :)
OTOH, if you really want to use the bracket syntax in the replacing file...
$ cat test.js.config
alert({TEST})
alert({FIRST_NAME})
... and the key-value pair in the replacement file...
$ cat change.txt
TEST=50
FIRST_NAME=Alex
...you can write a sed command to change the key-value pair in sed replacements...
$ sed 's#\(.*\)=\(.*\)#s/{\1}/\2/g#' change.txt
s/{TEST}/50/g
s/{FIRST_NAME}/Alex/g
...and use this command in the real replacement:
$ sed "$(sed 's#\(.*\)=\(.*\)#s/{\1}/\2/g#' change.txt)" test.js.config > test.js
$ cat test.js
alert(50)
alert(Alex)