Make ShellScript run with custom CLI command [duplicate]












1















This question already has an answer here:




  • How to run scripts without typing the full path?

    10 answers




I have a shell script i use frequently to manually clear the dentries, inodes and page cache in my RAM: ramflush.sh



#!/bin/bash
echo " ██▀███ ▄▄▄ ███▄ ▄███▓ "
echo "▓██ ▒ ██▒▒████▄ ▓██▒▀█▀ ██▒ _____ "
echo "▓██ ░▄█ ▒▒██ ▀█▄ ▓██ ▓██░ | | F"
echo "▒██▀▀█▄ ░██▄▄▄▄██ ▒██ ▒██ | | L "
echo "░██▓ ▒██▒ ▓█ ▓██▒▒██▒ ░██▒ | | U"
echo "░ ▒▓ ░▒▓░ ▒▒ ▓▒█░░ ▒░ ░ ░ ___| S _"
echo " ░▒ ░ ▒░ ▒ ▒▒ ░░ ░ ░ || ____H__ -( (-"
echo " ░░ ░ ░ ▒ ░ ░ |_'(-------) '-'"
echo " ░ ░ ░ ░ | /"
echo "___________VERSION 1.0______________,-__..__|_____"
echo " "
read -p "[*] Do you have a need to flush?: " yn
case $yn in
[Yy]* ) ;;
[Nn]* ) echo "[X] Understood."; exit;;
* ) echo "[X] No input detected. Exiting."; exit;;
esac

echo " "
echo " <=== OPTIONS ===>"
echo " "
echo "1. Clear RAM Page Cache."
echo "2. Clear Dentries and Inodes."
echo "3. Clear Page Cache, Dentries and Inodes."
echo " "
read -p "[*] Choose what to flush: " ans

case $ans in
[1]* ) echo 1 > /proc/sys/vm/drop_caches; echo "[*] Cache Cleared.";;
[2]* ) echo 2 > /proc/sys/vm/drop_caches; printf "[*]Dentries Cleared.n[*]Inodes Cleared.n";;
[3]* ) echo 3 > /proc/sys/vm/drop_caches; printf "[*]Page Cache Clearedn[*]Dentries Cleared.n[*]Inodes Cleared.n";;
* ) echo "[X] No input detected. Exiting."; exit;;

esac


However it gets tiresome constantly changing back to my home directory, then going in to a folder and calling the script. I also refuse to just do the commands manually because it defies the point to me having made the script.



Is there a way I can add this to my bash shell in order to be able to run the script from any directory simply by typing ramflush in order to call and run this script, similar to nmap or ping?



Would I have to add it to the package manager and how do I go about doing this?










share|improve this question













marked as duplicate by muru bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 22:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • In your ~/.bash_profile (or ~/.profile, whichever you use), add the script's directory to your PATH. Then, assuming the script file is executable, you can run it from anywhere.
    – glenn jackman
    Nov 12 '18 at 22:11






  • 1




    Place it in the ~/bin directory and add it to your path or place it in the /usr/local/bin folder which is already on your path
    – George Udosen
    Nov 12 '18 at 22:11












  • You might want to investigate the select bash command to present your menu.
    – glenn jackman
    Nov 12 '18 at 22:12
















1















This question already has an answer here:




  • How to run scripts without typing the full path?

    10 answers




I have a shell script i use frequently to manually clear the dentries, inodes and page cache in my RAM: ramflush.sh



#!/bin/bash
echo " ██▀███ ▄▄▄ ███▄ ▄███▓ "
echo "▓██ ▒ ██▒▒████▄ ▓██▒▀█▀ ██▒ _____ "
echo "▓██ ░▄█ ▒▒██ ▀█▄ ▓██ ▓██░ | | F"
echo "▒██▀▀█▄ ░██▄▄▄▄██ ▒██ ▒██ | | L "
echo "░██▓ ▒██▒ ▓█ ▓██▒▒██▒ ░██▒ | | U"
echo "░ ▒▓ ░▒▓░ ▒▒ ▓▒█░░ ▒░ ░ ░ ___| S _"
echo " ░▒ ░ ▒░ ▒ ▒▒ ░░ ░ ░ || ____H__ -( (-"
echo " ░░ ░ ░ ▒ ░ ░ |_'(-------) '-'"
echo " ░ ░ ░ ░ | /"
echo "___________VERSION 1.0______________,-__..__|_____"
echo " "
read -p "[*] Do you have a need to flush?: " yn
case $yn in
[Yy]* ) ;;
[Nn]* ) echo "[X] Understood."; exit;;
* ) echo "[X] No input detected. Exiting."; exit;;
esac

echo " "
echo " <=== OPTIONS ===>"
echo " "
echo "1. Clear RAM Page Cache."
echo "2. Clear Dentries and Inodes."
echo "3. Clear Page Cache, Dentries and Inodes."
echo " "
read -p "[*] Choose what to flush: " ans

case $ans in
[1]* ) echo 1 > /proc/sys/vm/drop_caches; echo "[*] Cache Cleared.";;
[2]* ) echo 2 > /proc/sys/vm/drop_caches; printf "[*]Dentries Cleared.n[*]Inodes Cleared.n";;
[3]* ) echo 3 > /proc/sys/vm/drop_caches; printf "[*]Page Cache Clearedn[*]Dentries Cleared.n[*]Inodes Cleared.n";;
* ) echo "[X] No input detected. Exiting."; exit;;

esac


However it gets tiresome constantly changing back to my home directory, then going in to a folder and calling the script. I also refuse to just do the commands manually because it defies the point to me having made the script.



Is there a way I can add this to my bash shell in order to be able to run the script from any directory simply by typing ramflush in order to call and run this script, similar to nmap or ping?



Would I have to add it to the package manager and how do I go about doing this?










share|improve this question













marked as duplicate by muru bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 22:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • In your ~/.bash_profile (or ~/.profile, whichever you use), add the script's directory to your PATH. Then, assuming the script file is executable, you can run it from anywhere.
    – glenn jackman
    Nov 12 '18 at 22:11






  • 1




    Place it in the ~/bin directory and add it to your path or place it in the /usr/local/bin folder which is already on your path
    – George Udosen
    Nov 12 '18 at 22:11












  • You might want to investigate the select bash command to present your menu.
    – glenn jackman
    Nov 12 '18 at 22:12














1












1








1








This question already has an answer here:




  • How to run scripts without typing the full path?

    10 answers




I have a shell script i use frequently to manually clear the dentries, inodes and page cache in my RAM: ramflush.sh



#!/bin/bash
echo " ██▀███ ▄▄▄ ███▄ ▄███▓ "
echo "▓██ ▒ ██▒▒████▄ ▓██▒▀█▀ ██▒ _____ "
echo "▓██ ░▄█ ▒▒██ ▀█▄ ▓██ ▓██░ | | F"
echo "▒██▀▀█▄ ░██▄▄▄▄██ ▒██ ▒██ | | L "
echo "░██▓ ▒██▒ ▓█ ▓██▒▒██▒ ░██▒ | | U"
echo "░ ▒▓ ░▒▓░ ▒▒ ▓▒█░░ ▒░ ░ ░ ___| S _"
echo " ░▒ ░ ▒░ ▒ ▒▒ ░░ ░ ░ || ____H__ -( (-"
echo " ░░ ░ ░ ▒ ░ ░ |_'(-------) '-'"
echo " ░ ░ ░ ░ | /"
echo "___________VERSION 1.0______________,-__..__|_____"
echo " "
read -p "[*] Do you have a need to flush?: " yn
case $yn in
[Yy]* ) ;;
[Nn]* ) echo "[X] Understood."; exit;;
* ) echo "[X] No input detected. Exiting."; exit;;
esac

echo " "
echo " <=== OPTIONS ===>"
echo " "
echo "1. Clear RAM Page Cache."
echo "2. Clear Dentries and Inodes."
echo "3. Clear Page Cache, Dentries and Inodes."
echo " "
read -p "[*] Choose what to flush: " ans

case $ans in
[1]* ) echo 1 > /proc/sys/vm/drop_caches; echo "[*] Cache Cleared.";;
[2]* ) echo 2 > /proc/sys/vm/drop_caches; printf "[*]Dentries Cleared.n[*]Inodes Cleared.n";;
[3]* ) echo 3 > /proc/sys/vm/drop_caches; printf "[*]Page Cache Clearedn[*]Dentries Cleared.n[*]Inodes Cleared.n";;
* ) echo "[X] No input detected. Exiting."; exit;;

esac


However it gets tiresome constantly changing back to my home directory, then going in to a folder and calling the script. I also refuse to just do the commands manually because it defies the point to me having made the script.



Is there a way I can add this to my bash shell in order to be able to run the script from any directory simply by typing ramflush in order to call and run this script, similar to nmap or ping?



Would I have to add it to the package manager and how do I go about doing this?










share|improve this question














This question already has an answer here:




  • How to run scripts without typing the full path?

    10 answers




I have a shell script i use frequently to manually clear the dentries, inodes and page cache in my RAM: ramflush.sh



#!/bin/bash
echo " ██▀███ ▄▄▄ ███▄ ▄███▓ "
echo "▓██ ▒ ██▒▒████▄ ▓██▒▀█▀ ██▒ _____ "
echo "▓██ ░▄█ ▒▒██ ▀█▄ ▓██ ▓██░ | | F"
echo "▒██▀▀█▄ ░██▄▄▄▄██ ▒██ ▒██ | | L "
echo "░██▓ ▒██▒ ▓█ ▓██▒▒██▒ ░██▒ | | U"
echo "░ ▒▓ ░▒▓░ ▒▒ ▓▒█░░ ▒░ ░ ░ ___| S _"
echo " ░▒ ░ ▒░ ▒ ▒▒ ░░ ░ ░ || ____H__ -( (-"
echo " ░░ ░ ░ ▒ ░ ░ |_'(-------) '-'"
echo " ░ ░ ░ ░ | /"
echo "___________VERSION 1.0______________,-__..__|_____"
echo " "
read -p "[*] Do you have a need to flush?: " yn
case $yn in
[Yy]* ) ;;
[Nn]* ) echo "[X] Understood."; exit;;
* ) echo "[X] No input detected. Exiting."; exit;;
esac

echo " "
echo " <=== OPTIONS ===>"
echo " "
echo "1. Clear RAM Page Cache."
echo "2. Clear Dentries and Inodes."
echo "3. Clear Page Cache, Dentries and Inodes."
echo " "
read -p "[*] Choose what to flush: " ans

case $ans in
[1]* ) echo 1 > /proc/sys/vm/drop_caches; echo "[*] Cache Cleared.";;
[2]* ) echo 2 > /proc/sys/vm/drop_caches; printf "[*]Dentries Cleared.n[*]Inodes Cleared.n";;
[3]* ) echo 3 > /proc/sys/vm/drop_caches; printf "[*]Page Cache Clearedn[*]Dentries Cleared.n[*]Inodes Cleared.n";;
* ) echo "[X] No input detected. Exiting."; exit;;

esac


However it gets tiresome constantly changing back to my home directory, then going in to a folder and calling the script. I also refuse to just do the commands manually because it defies the point to me having made the script.



Is there a way I can add this to my bash shell in order to be able to run the script from any directory simply by typing ramflush in order to call and run this script, similar to nmap or ping?



Would I have to add it to the package manager and how do I go about doing this?





This question already has an answer here:




  • How to run scripts without typing the full path?

    10 answers








command-line bash scripts 18.10






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 22:02







user797940











marked as duplicate by muru bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 22:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by muru bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 '18 at 22:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • In your ~/.bash_profile (or ~/.profile, whichever you use), add the script's directory to your PATH. Then, assuming the script file is executable, you can run it from anywhere.
    – glenn jackman
    Nov 12 '18 at 22:11






  • 1




    Place it in the ~/bin directory and add it to your path or place it in the /usr/local/bin folder which is already on your path
    – George Udosen
    Nov 12 '18 at 22:11












  • You might want to investigate the select bash command to present your menu.
    – glenn jackman
    Nov 12 '18 at 22:12


















  • In your ~/.bash_profile (or ~/.profile, whichever you use), add the script's directory to your PATH. Then, assuming the script file is executable, you can run it from anywhere.
    – glenn jackman
    Nov 12 '18 at 22:11






  • 1




    Place it in the ~/bin directory and add it to your path or place it in the /usr/local/bin folder which is already on your path
    – George Udosen
    Nov 12 '18 at 22:11












  • You might want to investigate the select bash command to present your menu.
    – glenn jackman
    Nov 12 '18 at 22:12
















In your ~/.bash_profile (or ~/.profile, whichever you use), add the script's directory to your PATH. Then, assuming the script file is executable, you can run it from anywhere.
– glenn jackman
Nov 12 '18 at 22:11




In your ~/.bash_profile (or ~/.profile, whichever you use), add the script's directory to your PATH. Then, assuming the script file is executable, you can run it from anywhere.
– glenn jackman
Nov 12 '18 at 22:11




1




1




Place it in the ~/bin directory and add it to your path or place it in the /usr/local/bin folder which is already on your path
– George Udosen
Nov 12 '18 at 22:11






Place it in the ~/bin directory and add it to your path or place it in the /usr/local/bin folder which is already on your path
– George Udosen
Nov 12 '18 at 22:11














You might want to investigate the select bash command to present your menu.
– glenn jackman
Nov 12 '18 at 22:12




You might want to investigate the select bash command to present your menu.
– glenn jackman
Nov 12 '18 at 22:12










2 Answers
2






active

oldest

votes


















2














You can drop that script as ramflush without the sh extenion in two places:





  1. ~/bin or


  2. /usr/local/bin,


for the first case add that path in .bashrc with the line export PATH=$PATH:$HOME/bin and the other is already in your path. Now you can simply type ramflush.






share|improve this answer





















  • Thankyou for all the help. I've accepted @GeorgeUdosen 's answer as the best answer because of method two, which, from my perspective as a rookie to shell scripting - is the most universal method and easy to incorporate in a shell script to make this happen automatically. When I consulted that directory it appears that several tools have coined this method all ready.
    – user797940
    Nov 12 '18 at 22:23



















3














The quickest way to do this, would be using an alias.



Add this to your .bashrc:



alias ramflush='/path/to/your/script/ramflush.sh'


Then open a new terminal or run source ~/.bashrc to reload your bash configuration.



You can now call ramflush anywhere in the terminal.






share|improve this answer

















  • 2




    Requires the script is executable. Alternately alias ramflush='bash /path/to/your/script/ramflush.sh'
    – glenn jackman
    Nov 12 '18 at 22:14

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can drop that script as ramflush without the sh extenion in two places:





  1. ~/bin or


  2. /usr/local/bin,


for the first case add that path in .bashrc with the line export PATH=$PATH:$HOME/bin and the other is already in your path. Now you can simply type ramflush.






share|improve this answer





















  • Thankyou for all the help. I've accepted @GeorgeUdosen 's answer as the best answer because of method two, which, from my perspective as a rookie to shell scripting - is the most universal method and easy to incorporate in a shell script to make this happen automatically. When I consulted that directory it appears that several tools have coined this method all ready.
    – user797940
    Nov 12 '18 at 22:23
















2














You can drop that script as ramflush without the sh extenion in two places:





  1. ~/bin or


  2. /usr/local/bin,


for the first case add that path in .bashrc with the line export PATH=$PATH:$HOME/bin and the other is already in your path. Now you can simply type ramflush.






share|improve this answer





















  • Thankyou for all the help. I've accepted @GeorgeUdosen 's answer as the best answer because of method two, which, from my perspective as a rookie to shell scripting - is the most universal method and easy to incorporate in a shell script to make this happen automatically. When I consulted that directory it appears that several tools have coined this method all ready.
    – user797940
    Nov 12 '18 at 22:23














2












2








2






You can drop that script as ramflush without the sh extenion in two places:





  1. ~/bin or


  2. /usr/local/bin,


for the first case add that path in .bashrc with the line export PATH=$PATH:$HOME/bin and the other is already in your path. Now you can simply type ramflush.






share|improve this answer












You can drop that script as ramflush without the sh extenion in two places:





  1. ~/bin or


  2. /usr/local/bin,


for the first case add that path in .bashrc with the line export PATH=$PATH:$HOME/bin and the other is already in your path. Now you can simply type ramflush.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 22:16









George Udosen

19.8k94267




19.8k94267












  • Thankyou for all the help. I've accepted @GeorgeUdosen 's answer as the best answer because of method two, which, from my perspective as a rookie to shell scripting - is the most universal method and easy to incorporate in a shell script to make this happen automatically. When I consulted that directory it appears that several tools have coined this method all ready.
    – user797940
    Nov 12 '18 at 22:23


















  • Thankyou for all the help. I've accepted @GeorgeUdosen 's answer as the best answer because of method two, which, from my perspective as a rookie to shell scripting - is the most universal method and easy to incorporate in a shell script to make this happen automatically. When I consulted that directory it appears that several tools have coined this method all ready.
    – user797940
    Nov 12 '18 at 22:23
















Thankyou for all the help. I've accepted @GeorgeUdosen 's answer as the best answer because of method two, which, from my perspective as a rookie to shell scripting - is the most universal method and easy to incorporate in a shell script to make this happen automatically. When I consulted that directory it appears that several tools have coined this method all ready.
– user797940
Nov 12 '18 at 22:23




Thankyou for all the help. I've accepted @GeorgeUdosen 's answer as the best answer because of method two, which, from my perspective as a rookie to shell scripting - is the most universal method and easy to incorporate in a shell script to make this happen automatically. When I consulted that directory it appears that several tools have coined this method all ready.
– user797940
Nov 12 '18 at 22:23













3














The quickest way to do this, would be using an alias.



Add this to your .bashrc:



alias ramflush='/path/to/your/script/ramflush.sh'


Then open a new terminal or run source ~/.bashrc to reload your bash configuration.



You can now call ramflush anywhere in the terminal.






share|improve this answer

















  • 2




    Requires the script is executable. Alternately alias ramflush='bash /path/to/your/script/ramflush.sh'
    – glenn jackman
    Nov 12 '18 at 22:14
















3














The quickest way to do this, would be using an alias.



Add this to your .bashrc:



alias ramflush='/path/to/your/script/ramflush.sh'


Then open a new terminal or run source ~/.bashrc to reload your bash configuration.



You can now call ramflush anywhere in the terminal.






share|improve this answer

















  • 2




    Requires the script is executable. Alternately alias ramflush='bash /path/to/your/script/ramflush.sh'
    – glenn jackman
    Nov 12 '18 at 22:14














3












3








3






The quickest way to do this, would be using an alias.



Add this to your .bashrc:



alias ramflush='/path/to/your/script/ramflush.sh'


Then open a new terminal or run source ~/.bashrc to reload your bash configuration.



You can now call ramflush anywhere in the terminal.






share|improve this answer












The quickest way to do this, would be using an alias.



Add this to your .bashrc:



alias ramflush='/path/to/your/script/ramflush.sh'


Then open a new terminal or run source ~/.bashrc to reload your bash configuration.



You can now call ramflush anywhere in the terminal.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 22:13









Wayne_Yux

3,97131428




3,97131428








  • 2




    Requires the script is executable. Alternately alias ramflush='bash /path/to/your/script/ramflush.sh'
    – glenn jackman
    Nov 12 '18 at 22:14














  • 2




    Requires the script is executable. Alternately alias ramflush='bash /path/to/your/script/ramflush.sh'
    – glenn jackman
    Nov 12 '18 at 22:14








2




2




Requires the script is executable. Alternately alias ramflush='bash /path/to/your/script/ramflush.sh'
– glenn jackman
Nov 12 '18 at 22:14




Requires the script is executable. Alternately alias ramflush='bash /path/to/your/script/ramflush.sh'
– glenn jackman
Nov 12 '18 at 22:14



Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python