How to run an alias in a shell script?

1

I made aliases to edit and retrieve branch descriptions:

Those are nifty, but I also want to check the description for all branches at once. So I have this script:

That works as I want, but I would prefer to keep the command in my .gitconfig instead of a separate script file. I had to create this separately because trying to create an alias with the "!f(){}; f" strategy would always fail... I'd receive an error about "bad substitution".

After some research, I discovered this is *probably* because git's bang syntax spawns the sh shell, and sh does not handle complicated substitutions (like ${line/\*/}), or almost any other string manipulation. Note that the git-about alias also uses a substitution, but that one has no issues.

Question: Is there a way to have git spawn an arbitrary shell when running an external command as an alias?

Alternatively, is there a way to rewrite the script without using non-sh...

0 0
2
#!/bin/bashchmod +x script.shexport PATH=$PATH:/appropriate/directorysource source script.sh

(typically, you want $HOME/bin for storing your own scripts)

Add a "shebang" at the top of your file:

Hey thanks for your reply. I tried that out. Now I can run it without the "sh" command. But I still have to prefix the command with "./" which I don't want to. :)

If you are sure the script folder is in PATH, and it still does not work without ./, then you may have a name collision. Try renaming.

See comments about the path. You need to put your script in a directory which you have to append to your PATH.

source ./my-cool-script (or . ./my-cool-script) will pollute the current shell session with any variables defined in the script. Sourcing should not be used unless you want to explicitly modify the shell session by setting environment vars, changing the...

0 0
3

Definin an alias on Linux system is very simple.

From the following example we see that: the I_am_only_ls_alias alias command gives us the output as ls command

# alias I_am_only_ls_alias=ls # I_am_only_ls_alias

Output:

file file1

But when I trying to do the same in bash script (define alias I_am_only_ls_alias), I get I_am_only_ls_alias: command not found.

Example of my bash script:

alias_test.bash

#!/bin/bash alias I_am_only_ls_alias=ls I_am_only_ls_alias

Run the bash script - alias_test.bash

/tmp/alias_test.bash

Output:

/tmp/: line 88: I_am_only_ls_alias: command not found

So, first I want to ask:

Why doesn't bash recognize the command I_am_only_ls_alias as an alias?

And what do I need to do in order to define aliases inside a bash script? Is it...

0 0
4
I'm trying to set an alias that applies to the current shell (the shell I'm running the script from) from a shell script. The alias is for cd-ing into the folder of the script. Here's the (not working) script: #!/bin/bash shopt -s expand_aliases DIR=$(cd $(dirname "$0"); pwd) # Detect the folder of the script. alias cdr="cd $DIR" # cd into the folder. I quickly realized that this didn't work because the alias it made was pertinent to the script's subshell. Then, I tried to source the file (as in . makeAlias.sh). However, this produced an error: dirname: illegal option -- b. How do I write a bash script that makes an alias relevant to the outer shell (the shell running the script)? The immediate problem is that the value of $0 is now -bash. You might want to refactor your code to use a different reference point, or simply hard-code the path. To answer the "how do I ...?" you aren't doing anything wrong, it's just that the logic has to be adapted to a different environment --...
0 0
5

On an amazon linux instance, I have two scripts called start_my_app and stop_my_app which start and stop forever (which in turn run my node.js app). I use these scripts to manually start and stop my node app. So far so good.

My problem: I also want to set it up such that start_my_app is run whenever the system boots up. I know that I need to add a file inside init.d and I know how to symlink it to the proper directory within rc.d, but can’t figure out what actually needs to go inside the file that I place in init.d. I’m thinking it should be just one line, like, start_my_app, but that hasn’t been working for me.

Any help would be appreciated. Thanks.

AutoIT – How to run shell script

I managed to run below shell script code in applescript and the outpur generated correctly. Shell Script. do shell script mdls ‘UAT.pdf’ | grep ‘kMDItemPageWidth//|kMDItemPageHeight’ | awk ‘/ = / {f

How can I run a shell script on only the next startup? Is it...

0 0
6

When you want to save yourself from typing an unwieldy command over and over again you can create and use an alias for it. It will then act as a shortcut to the larger command, which you can type and run instead.

Creating aliases in UNIX (and Linux) is done with a simple alias command which follows this format: alias name='command you want to run'.

Replace the "name" with your shortcut command, and "command you want to run" with the larger command you want to create an alias of. Here's a simple example:

alias accesslog='tail -f /var/log/lighttpd/access.log'

In this example I've effectively created a new accesslog command which is an alias of the tail -f /var/log/lighttpd/access.log command. What it does is follow the access.log file and display new entries in it as they happen. Now instead of having to write the whole tail -f command every time I want to look at what's happening in the access.log file I can simply run the accesslog alias command instead, which...

0 0
7

The simple answer for you is that scripts create non-interactive shells and, by default, the expand_aliases option is often disabled.

You can fix this very simply by just adding the following line to the top of your script to enable the alias expansion:

shopt -s expand_aliases

This problem has been bugging me, so I did research and then wrote a blog post once I figured out how to fix it for myself: Post about using alias from within Linux shell scripts.

Of course, right after I figured out that part, I found that, while it works for what you need, it will not work if you have a subshell within a a subshell. I am still looking into the fix for that problem, that is how I just came across your question. On the blog post, I mention a cheap hack that I use to grab the alias in a shell script. It isn't elegant, but it actually works even in this multiple subshell problem I...

0 0
8

You can run a shell script in infinite loop by using while loop.

#!/bin/bash while true do echo "Press CTRL+C to stop the script execution" # Enter your desired command in this block. done

You can also do this using below inline command

while true; do echo 'Press CTRL+C to stop the script execution'; done

You can also run an infinite loop in the background

while /bin/true; do something_in_the_background done &

Reference:
https://unix.stackexchange.com/questions/223778/how-to-run-an-infinite-loop-in-the-background

http://techieroop.com/how-to-run-a-shell-script-in-infinite-loop/ Scriptingbash scripting,scripting,shell script

You can run a shell script in infinite loop by using while loop. #!/bin/bash while true do echo 'Press CTRL+C to stop the script execution' # Enter your desired command in this block. done You can also do this using below inline command while true; do echo 'Press CTRL+C to stop the script execution'; done You can also run...

...
0 0
9

Questions:

I’m trying to write a small script to change the current directory to my project directory:

#!/bin/bash cd /home/tree/projects/java

I saved this file as proj, added execute permission with chmod, and copied it to /usr/bin. When I call it by:
proj, it does nothing. What am I doing wrong?

Answers:

Shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is. The cd succeeds, but as soon as the subshell exits, you’re back in the interactive shell and nothing ever changed there.

One way to get around this is to use an alias instead:

alias proj="cd /home/tree/projects/java"

Questions:

Answers:

You’re doing nothing wrong! You’ve changed the directory, but only within the subshell that runs the script.

You can run the script in your current process with the “dot” command:

. proj

But I’d prefer Greg’s suggestion to use an alias in this simple...

0 0
10

If you execute it in a script, the alias will be over by the time the script finishes executing.

In case you want it to be permanent:

Your alias is well defined, but you have to store it in ~/.bashrc, not in a shell script.

Add it to that file and then source it with . .bashrc - it will load the file so that alias will be possible to use.

In case you want it to be used just in current session:

Just write it in your console prompt.

$ aa The program 'aa' is currently not installed. ... $ $ alias aa="echo hello" $ $ aa hello $

Also: From Kent answer we can see that you can also source it by source your_file. In that case you do not need to use a shell script, just a normal file will make...

0 0
11

In this example the file will be called myShell

First of all we will need to make this file we can just start off by typing the following:

sudo nano myShell

Notice we didn't put the .sh extension? That's because when we run it from the terminal we will only need to type myShell in order to run our command!

Now, in nano the top line MUST be #!/bin/bash then you may leave a new line before continuing.

For demonstration I will add a basic Hello World! response

So, I type the following:

echo Hello World!

After that my example should look like this:

#!/bin/bash echo Hello World!

Now save the file and then run this command:

sudo chmod +x myShell

Now we have made the file executable we can move it to /usr/bin/ by using the following command:

sudo cp myShell /usr/bin/

Just to make sure that the machine can execute it properly we will need to reboot the machine

I used sudo shutdown -r...

0 0
12

Shell Arithmetic

Use to perform arithmetic operations.

Syntax:
expr op1 math-operator op2

Examples:
$ expr 1 + 3
$ expr 2 - 1
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 \* 3
$ echo `expr 6 + 3`

Note:
expr 20 %3 - Remainder read as 20 mod 3 and remainder is 2.
expr 10 \* 3 - Multiplication use \* and not * since its wild card.

For the last statement not the following points

(1) First, before expr keyword we used ` (back quote) sign not the (single quote i.e. ') sign. Back quote is generally found on the key under tilde (~) on PC keyboard OR to the above of TAB key.

(2) Second, expr is also end with ` i.e. back quote.

(3) Here expr 6 + 3 is evaluated to 9, then echo command prints 9 as sum

(4) Here if you use double quote or single quote, it will NOT work
For e.g.
$ echo "expr 6 + 3" # It will print expr 6 + 3
$ echo 'expr 6 + 3' # It will print expr 6 +...

0 0
13
How to Run a script - PowerShell - SS64.com

There are several ways to run a PowerShell script.

Before running any scripts on a new PowerShell installation, you must first set an appropriate Execution Policy,
e.g. Set-ExecutionPolicy RemoteSigned

A PowerShell script is the equivalent of a Windows CMD or MS-DOS batch file, the file should be saved with a .ps1 extension, e.g. MyScript.ps1

Call or Invoke a script to run it

The most common (default) way to run a script is by calling it:

PS C:\> & "C:\Batch\My first Script.ps1"

PS C:\> & cscript /nologo "C:\Batch\another.vbs"

If the path does not contain any spaces, then you can omit the quotes and the '&' operator

PS C:\> C:\Batch\Myscript.ps1

If the script is in the current directory, you can omit the path but must instead explicitly indicate the current directory using .\ (or ./ will also work)

PS C:\> .\Myscript.ps1

When you invoke a script using...

0 0
14

Typically sudo will ignore any aliased commands from your .bashrc, .bash_aliases, or the alias command. For example, I use "ll" as an alias for "ls -lh". Typing "ll" will give me a long-listing of a directory's contents, while typing "sudo ll" will give me:

I learned this when I tried to create an alias for "shutdown" that would refuse to shutdown if rtorrent was running. Unfortunately you need root privileges to use /sbin/shutdown, and sudo would completely ignore the clever script I aliased as "shutdown". The solution is an additional alias:

The space following "sudo" tells bash to check if the command that follows the space is also an alias. From the bash man page:

Now my aliased shutdown script is called even though it's being run with sudo. I've heard of another solution (also an alias), but I haven't tried it (1) because this one works just fine, and (2) because I don't understand why it works (if it does work). Here it is:

I'm not sure if it's...

0 0
15

Check out my other tutorials on the Unix Page, and my

Check my blog

Copyright 1994, 1995 Bruce Barnett and General Electric Company

Copyright 2001, 2013 Bruce Barnett

All rights reserved

You are allowed to print copies of this tutorial for your personal use, and link to this page, but you are not allowed to make electronic copies, or redistribute this tutorial in any form without permission.

Original version written in 1994 and published in the Sun Observer

This section describes C Shell (CSH/TCSH) programming. It covers conditional testing, control loops, and other advanced techniques.

This month begins a tutorial on the bad-boy of UNIX, lowest of the low, the shell of last resort. Yes, I am talking about the C shell. FAQ's flame it. Experts have criticized it. Unfortunately, this puts UNIX novices in an awkward situation. Many people are given the C shell as their default shell. They aren't familiar with it, but they have...

0 0
16
...
0 0
17

There are two ways of making a file executable:

GUI Method:

Go to the permissions tab, then tick the box `Execute: [ ] Allow executing file as program.

Terminal / Command method:

You can either use:

cd /to/my/required/directory

Then run

chmod +x filename.extension

Or just run:

chmod +x /path/to/your/filename.extension

chmod does also have some more advanced options:

The spaces are to show that it is split up: - rwx --- ---

The first set of --- is User. The second is Group and the last is Other (anyone else)

r stands for Read, w for Write and x for eXecute.

So to allow everyone to read it, but only Group to execute and User to read and write it (but for some reason not execute) would be:

-rw- rx- r-- But this would be added to the command as:

chmod +rw-rx-r-- /path/to/file.extension

chmod also can do this in numbers. It is based on binary (I think, as it is...

0 0