BASH shell scripting using parameters - linux

I am trying to create three BASH scripts to help organize my terminals and workspace on my VM.
Below are the commands that I wish to have parameterized.
1) How to rename the title of a terminal window:
PS1='\[\e]0;TERMINAL NAME\a\]${debian_chroot:+($debian_chroot)}\u#\h:\w\$ '
2) To change the number of workspaces:
gsettings set org.gnome.desktop.wm.preferences num-workspaces "5"
3) To rename the name of Workspaces:
gsettings set org.gnome.desktop.wm.preferences workspace-names "['IntelliJ', 'Cloud Discovery', 'Full Stack', 'Simulators', 'Misc']"
It's been a little while since I have done BASH scripting and I am forgetting how to run a console command via a BASH script.
My initial attempts have all failed. Here is the code that I am written most recently:
#!/bin/bash
export cmd="PS1='\[\e]0;$1\a\]${debian_chroot:+($debian_chroot)}\u#\h:\w\$ '"
echo {$cmd}
./setNewTermName MyName
But when I run the script as above, it is not renaming the terminal. However, when I run the PS1 command above directly on the command like (substituting in the actual name), it works.
Any ideas on what I am doing wrong?

You need to define a local variable that will get the value of the title as an argument as under. Then you will use that local variable (title in this case) in your PS1 variable.
Then, in order to run the script, you need to "source" the script in the current shell so that the variable in the script are visible to the parent shell.
#!/bin/bash
title=$1;
PS1='\[\e]0;${title}\a\]${debian_chroot:+($debian_chroot)}\u#\h:\w\$ '
So, instead of running the script as
./setNewTermName MyName
Run without the / as under. Notice the space after the period:
. setNewTermName MyName
Or as:
source setNewTermName MyName

Related

How to use a variable or name instead of path of the file in a command in linux?

I have a shell script sample.sh. Inside the shell script, there are many commands, it looks like this for example:
#!/bin/bash
command 1 ......
command 2 ......
command 3 ......
txt1="/users/doc/folder1/sam.txt"
txt2="/users/doc/folder2/pam.txt"
txt3="/users/doc/folder3/ram.txt"
echo "run done"
First I gave a run with this script like sh sample.sh. After running this shell script I want to run a command in which I wanted to give txt3 which was the name I'm using instead of the path for the file ram.txt
The command looks like this for eg:
convert -i txt3 > sim.tsv
This gave me an error. Error: The requested txt file (txt3) could not be opened. Exiting!
May I know how this works without giving the path to the file in the command?
You need to source the script, not run it, and export your variables to have them persist after the script finished.
You have to refer to the variable as $txt3 after the sourcing.

Error while trying set Environment variables in linux using shell script

So I am trying to set environment variables using my shell Script. The script takes some inputs from the user and then i have to set those inputs in the environment variable. I am using two shell Script for the same but i am getting permission denied errors.
Script 1
DEFAULT_NAME="sample"
read -p "Enter your Name: [$DEFAULT_NAME]: " USER_NAME
if [ -z "$USER_NAME" ]
then
USER_NAME=$DEFAULT_NAME
else
USER_NAME=$USER_NAME
fi
source setEnv.sh
Script 2
echo -e "export NAME=${USER_NAME}" >> /etc/profile.d/nameenv.sh
First, the if condition in Script 1 is wrong, you probably want to test $USER_NAME instead.
If you are using bash, you can replace the whole if statement with:
USER_NAME=${USER_NAME:-$DEFAULT_NAME}
In Script 2 are you sure that you want to append a new line to /etc/profile.d/nameenv.sh, every time you execute the script? The last declaration will hide the preceding ones.
Finally, note that you need root privilege to write in /etc/profile.d. Are you running the script as a privileged user?
[Edit] Trying to guess what you are trying to do here. If you need that USER_NAME is redefined for the user's current session (and not system-wide), just replace the last line in Script 1 with:
export USER_NAME
and remove Script 2. If you want to make it permanent (again, for the current user only), modify Script 2 to write the variable declaration in ~/.bash_profile instead.

Setting console env using a shell script

I have a shell script setmyenv.sh as below
#!/bin/sh
export PATH=./abc/tools:$PATH
env | grep PATH
When I run it sh setmyenv.sh, I could see that the PATH env is set accordingly.
PATH=./abc/tools:<whatever my existing PATH setting>
However, after my command finish, if I manually type env | grep PATH on the console, I got
PATH=<whatever my existing PATH setting>
I lost the setting that I set using setmyenv.sh
It looks like the environement is only set in the lifetime of my script run.
How could I have the environment set sticky even after the script ended. i.e. the purpose of the script is to set the environment.?
P/S: I don't want to set it in my .bash_profile nor etc\profile, given I only want to set it when needed, by calling setmyenv.sh, but not every time I open my console. i.e. not per the answer of Using .sh script to set an environment variable or How to set global environment variables using shell script .sh
When you run
sh setmyenv.sh
it runs in a separate sh process and the changes to PATH are lost when the process finishes.
You need to source your script:
source setmyenv.sh
or
. setmyenv.sh
so that it runs in your current shell and all variable assignments are preserved. Remember not to have any exit in setmyenv.sh script. If you do, sourcing the script will terminate your shell.
See also:
Difference between sourcing a script vs executing it
What's a subshell

Difference with running a script using source and ./ [duplicate]

csh:
set a=0
echo "a is $a"
when i do ./my_script.csh output is:
a is
when i do source my_script.csh output is:
a is 0
Why is it so . As i know that ./ execution uses new shell.
That's right, ./my_script.csh starts a new shell, and uses the #! that you should have at the top of the file to select which shell to run (which should be csh in this case).
source my_script.csh runs the script in the current shell.
If the script is incorrectly run in, for example, the bash shell, set a=0 is not the syntax for setting an environment variable in bash, so the code won't work as you expected, because you're using the wrong shell.
Take a look at the #! at the top of the file. Is it correct?
check if variable "a" is set in your current shell:
set | grep '^a='
Remember that once you source script to your current shell,
all it's global variables are there until unset or you exit the current shell.
You may want to start a new shell, source the script, end exit shell to perform valid tests.
I don't know the context of your problem, but you may want to export some key variables to have their copies in every subprocess.

How to make declare in a Linux shell script?

I want to put below declare in a shell script: proxy_set
declare -x https_proxy="https://192.168.220.4:8080/"
And then I execute it like below.
$ ./proxy_set
But "export" shows nothing happened.
And in another way if I execute it like this:
$ source proxy_set
Then "export" shows it works!
My question is how can I make it work without additional "source" cmd?
Thanks!
You can't. Setting variables in the environment only affects the environment of that shell and any future children it spawns; there's no way to affect the parent shell. When you run it without the source (or .), a brand new shell is started up, then the variable is set in that shell's environment, and then that shell exits, taking its environment with it.
The source reads the commands and executes them within the current shell as if you had typed them.
So if you want to set environment variables in a script, you have to source it. Alternatively, you can have a command generate shell commands as output instead of running them, and then the parent can evaluate the output of the command. Things like ssh-agent use this approach.
Try just adding:
export https_proxy="https://192.168.220.4:8080/"
Then execute your script normally.

Resources