Execute alias in cshrc - linux

I have alias which contain small script. We generally call alias with username argument.
For small enhancement,we need to call from shell script. I have write into shell script but it looks like that it does not work
/usr/local/buildpkgs/latest/TclDevKit2.6/bin/tclsh
$VDK_PATH/scripts/ves.tcl /bin/csh $vdktmpfile !*;source
$vdktmpfile;/bin/rm $vdktmpfile;
/usr/local/buildpkgs/latest/TclDevKit2.6/bin/tclsh
$VDK_PATH/scripts/vep.tcl $vdktmpfile $SHELL "$prompt:q";
source $vdktmpfile; /bin/rm $vdktmpfile = sample
we generally invoke alias from command line like sample <username>.We need to call this alias from cshell scrip similar way sample <username>.
Does anyone help me to figure out this issue.
Note: I never seen such a long alias if anyone provide me detail it will be great help.

Where is the alias specified? If not in .bashrc but in a general shell script, then you will have to inside of your current shell script:
a) first invoke shell script containing the alias then
b) call alias as you normally do i.e sample <username>
Other alternative is you might want to call your shell script like this:
. ./<script-name>.sh <args>*
as opposed to just
./<script-name>.sh <args>*
the extra period is to execute script in current shell where alias already defined and not create its own process

Related

Want to create an bash alias which accepts function with argument

I want to create alias which would do something like that
ssh -i {location of file} username#host
sf() {
ssh -i ~/.ssh/key user2#$1
}
alias ssf=sf
and i have to
ssf host
If i do in this way, my autocomplete (zsh, iterm) will not work. Was thinking of calling a function in alias which accepts arguments. Any help?
A quick and simple method to creating an alias that accepts arguments would be something similar to this pseudo-code:
#!/bin/bash
declare -r -a n=('$1' '$2' '$3' '$4' '$5')
function main() {
command -i ${n[0]} -f ${n[1]} --another-arg ${n[2]}
}
main
If I want this script available to all users, I will usually sudo su to login as root, then I will make the file executable and save the script file to /usr/bin/ directory. This will allow you to use the name of the script as a terminal command. While root, apply the proper permissions to the script, chown root script.sh if you want only root user to have access. If you want a custom command name to call the script, name the script the alias you wish to use.
Explaining the pseudo-code
The declare -r -a is the syntax to create a read-only array. n= is the array variable which will hold the values for each argument you use with the tool. ('$1''$2''$3''$4''$5') are the argument variables up to 5 arguments. $1 will be the first argument, $2 will be the next, and onto the last. Each of these argument variable will hold the value you enter on the command line when using your script alias.
Next we have the main() function which will house the commands you wish to run. Inside this main() function you can see braces with the n variable with brackets holding a number such as ${n[0]}. What this is, is calling the value of your n array. Array values in bash are called with numbers starting with 0 rather than starting with 1. So 0 is the first value, or $1, and 1 is the second value, or $2, and so on.
.bashrc method
Another method for creating the alias in the same way is editing your .bashrc file for the user you are logged in under. So according to the user you would:
Enter cd command and hit enter to change to your home directory where .bashrc is located for the current user.
cd
Then using nano, open .bashrc.
nano .bashrc
Scroll to the bottom of the file and add the following:
alias your_alias="bash path/to/your/bash/script.sh'
Once the alias is set, type in ctrl+x to save the file. Nano will prompt you to confirm. Just type in y to confirm the save. Next you can reload .bashrc so you can use the command right away or you can reboot so it is available upon next boot. To reload .bashrc you will do the following:
source ~/.bashrc
And thats it! Your alias is ready. Now just use the alias as a normal CLI tool command, add your arguments as you need them, and hit enter and your script should work just fine.
Disclaimer
These methods may not be the most effective or the most secured for using your own aliases. However they should be sufficient to get you started. If anyone wishes to add to, or edit this answer to make it more informative and accurate (if it is inaccurate) feel free to do so. Hope this helps.

Aliases are not executed in Shell script

In my bashrc file I have n number of alias. But, If I execute via shell script,
it will not give expected output. Why it will be like this. Is there any way to
solve this problem.
Thanks in advance.
Aliases (as set using alias name=value) are only used in an interactive context, i. e. when the user types something on the command line. They are never executed by a script (unless a non-interactive shell is explicitly tweaked to do this using the shopt -s expand_aliases):
#!/bin/bash
alias ttt=date
ttt # will fail!
Sourcing a configuration script which defines aliases will not change anything about this. Scripts simply will not execute aliases.
To achieve what you want, rewrite your aliases as shell functions:
#!/bin/bash
ttt() {
date
}
ttt # will succeed!
Shell functions can replace aliases completely but there are some more things to know and consider:
You can even export shell functions so that child shells also have them. Use export -f ttt for this.
Shell functions can override other commands so they can interfere in the behaviour of scripts (unlike aliases which are never executed in scripts). Keep this in mind in case you plan to override things like cd or ls.
An overridden built-in of the shell (e. g. cd) can still be reached by calling it as builtin cd /my/direc/tory.
Argument handling is quite different from aliases (and much more powerful).

linux: passing arguments to the .bashrc file

I want to be able to run bash in different predefined configurations. for example: When i'm at work i'll do something like:
bash work
And it will load the appropriate environment variables.
The way i see it, the best way of doing it is passing the configuration name to the .bashrc command and than just configure it according to the name.
To do that, i need to pass the name to the .bashrc file.. How do i pass arguments to the .bashrc file from the bash command?
Of course alternatives for doing what i want will be appreciated!
Thank you!
From the man page:
--rcfile file
Execute commands from file instead of the standard personal initialization file ~/.bashrc if the shell is interactive (see INVOCATION below).
I don't think you can pass arguments to your individual bashrc directly, but you can call a common bashrc from your customized rc files and then either use arguments or global variables.
If you just want to set some environment variables, you can of course just manually call your customised rc file. To make this a little nicer from your command line, you can define a respective alias:
alias load_work='~/.workrc'
or with a new bash
alias load_work='bash --rcfile ~/.workrc'

shell export variable not come into effect

I (on mac osx) often use
export http_proxy=http://192.168.0.205:1099
to proxy http connection to get a highed download speed. To make things easy, I wrote a shell file named proxy.sh to do this:
#!/bin/sh
export http_proxy=http://192.168.0.205:1099
Before I downlaod, I execute proxy.sh shell command, but I found it did't not come into effect.It lost http_proxy variable in current commnad window(terminal). I must type export command in current terminal,it will come into effect.
So I want to know what's reason for this and a solution? thanks.
Running a shell script "normally" (with proxy.sh for example) results in that running in a sub-process so that it cannot affect the environment of the parent process.
Using . or source will run the shell script in the context of the current shell, so it will be able to affect the environment, using one of the following:
. proxy.sh
source proxy.sh
Another possibility (if you're using bash at least) is to create an alias to do the work for you. You can use something like:
alias faster='export http_proxy=http://192.168.0.205:1099'
so that you can then simply type faster on the command line and it will export that variable (in the context of the current shell).
You could also allow for one-shot settings such as:
alias faster='http_proxy=http://192.168.0.205:1099'
and then use:
faster your_program
which would translate into:
http_proxy=http://192.168.0.205:1099 your_program
That's a bash way to set a variable for just the one invocation of a command.
The export variable will only apply to the script -- if you want it to apply to the shell, you need to use source, and execute the script like so:
. ./proxy.sh
or:
source ./proxy.sh
Note the "." in the first example -- the dot follow by space means the script will apply to the shell.
The reason why your script does not work has been explained by Drakosha & how to make your script work has been explained by Anothony. But with the export in the script you need to source your script each time you open a new terminal. A better solution will be to add the export in .bash_profile or .bashrc
Hope this helps!
When executing a shell script a new shell is launched, the script is executed, and the shell dies. That's why you don't see the variable defined in your shell.
I suggest using an alias for the same purpose.

How can I define a bash alias as a sequence of multiple commands? [duplicate]

This question already has answers here:
Multiple commands in an alias for bash
(10 answers)
Closed 4 years ago.
I know how to configure aliases in bash, but is there a way to configure an alias for a sequence of commands?
I.e say I want one command to change to a particular directory, then run another command.
In addition, is there a way to setup a command that runs "sudo mycommand", then enters the password? In the MS-DOS days I'd be looking for a .bat file but I'm unsure of the linux (or in this case Mac OSX) equivalent.
For chaining a sequence of commands, try this:
alias x='command1;command2;command3;'
Or you can do this:
alias x='command1 && command2 && command3'
The && makes it only execute subsequent commands if the previous returns successful.
Also for entering passwords interactively, or interfacing with other programs like that, check out expect. (http://expect.nist.gov/)
You mention BAT files so perhaps what you want is to write a shell script. If so then just enter the commands you want line-by-line into a file like so:
command1
command2
and ask bash to execute the file:
bash myscript.sh
If you want to be able to invoke the script directly without typing "bash" then add the following line as the first line of the file:
#! /bin/bash
command1
command2
Then mark the file as executable:
chmod 755 myscript.sh
Now you can run it just like any other executable:
./myscript.sh
Note that unix doesn't really care about file extensions. You can simply name the file "myscript" without the ".sh" extension if you like. It's that special first line that is important. For example, if you want to write your script in the Perl programming language instead of bash the first line would be:
#! /usr/bin/perl
That first line tells your shell what interpreter to invoke to execute your script.
Also, if you now copy your script into one of the directories listed in the $PATH environment variable then you can call it from anywhere by simply typing its file name:
myscript.sh
Even tab-completion works. Which is why I usually include a ~/bin directory in my $PATH so that I can easily install personal scripts. And best of all, once you have a bunch of personal scripts that you are used to having you can easily port them to any new unix machine by copying your personal ~/bin directory.
it's probably easier to define functions for these types of things than aliases, keeps things more readable if you want to do more than a command or two:
In your .bashrc
perform_my_command() {
pushd /some_dir
my_command "$#"
popd
}
Then on the command line you can simply do:
perform_my_command my_parameter my_other_parameter "my quoted parameter"
You could do anything you like in a function, call other functions, etc.
You may want to have a look at the Advanced Bash Scripting Guide for in depth knowledge.
For the alias you can use this:
alias sequence='command1 -args; command2 -args;'
or if the second command must be executed only if the first one succeeds use:
alias sequence='command1 -args && command2 -args'
Your best bet is probably a shell function instead of an alias if the logic becomes more complex or if you need to add parameters (though bash supports aliases parameters).
This function can be defined in your .profile or .bashrc. The subshell is to avoid changing your working directory.
function myfunc {
( cd /tmp; command )
}
then from your command prompt
$ myfunc
For your second question you can just add your command to /etc/sudoers (if you are completely sure of what you are doing)
myuser ALL = NOPASSWD: \
/bin/mycommand
Apropos multiple commands in a single alias, you can use one of the logical operators to combine them. Here's one to switch to a directory and do an ls on it
alias x="cd /tmp && ls -al"
Another option is to use a shell function. These are sh/zsh/bash commands. I don't know enough of other shells to be sure if they work.
As for the sudo thing, if you want that (although I don't think it's a good idea), the right way to go is to alter the /etc/sudoers file to get what you want.
You can embed the function declaration followed by the function in the alias itself, like so:
alias my_alias='f() { do_stuff_with "$#" (arguments)" ...; }; f'
The benefit of this approach over just declaring the function by itself is that you can have a peace of mind that your function is not going to be overriden by some other script you're sourcing (or using .), which might use its own helper under the same name.
E.g., Suppose you have a script init-my-workspace.sh that you're calling like . init-my-workspace.sh or source init-my-workspace.sh whose purpose is to set or export a bunch of environment variables (e.g., JAVA_HOME, PYTHON_PATH etc.). If you happen to have a function my_alias inside there, as well, then you're out of luck as the latest function declaration withing the same shell instance wins.
Conversely, aliases have separate namespace and even in case of name clash, they are looked up first. Therefore, for customization relevant to interactive usage, you should only ever use aliases.
Finally, note that the practice of putting all the aliases in the same place (e.g., ~/.bash_aliases) enables you to easily spot any name clashes.
you can also write a shell function; example for " cd " and "ls " combo here

Resources