How to execute a script with using a keyword - linux

I have a requirement, where I have my linux script which does my work. But I want to use a simple keyword to execute my script file and get the output.
example:
sudo save
The above command should execute my script file which is saved as abc.sh
How to do it in linux or mac. Please help

Related

How to write a shell script file that can run a script in Linux

I am trying to make a .sh file that when clicked it runs the script inside. I am trying to recursively find a certain string value inside the contents of the files from a given folder, using $ grep -r "word" /home/folder_name but I don't know how to do so without running the script in terminal.
Any ideas for this?
Linux shell scripts can be written very basically.To make a shell script, start the script with #!/bin/sh and add normal linux commands. That is a simple explanation, but it is sufficient for most simple scenarios.
Example:
#!/bin/sh
echo "Hello, World!`

Robot Framework: How can I execute a .sh file in a test case?

I want to start a .sh file in a test case but it doesn't work like I would like it to.
I tried it with Run Process /appl/Test1/asd.sh Value1 Value2
and OperatingSystem.Run /Path/Test1/asd.sh Value1 Value2but both don't execute the asd.sh like it's executed in shell.
I used this Keywords for running a .pl Script and there it was no problem.
Do you mean to run .sh script in the same machine you launch ROBOT Framework or running the .sh file in another target unix/linux machine?
Solution for these two(2) scenario as below. You can do many thing with this RUN PROCESS keyword, please refer to this link for RUN PROCESS keyword: http://robotframework.org/robotframework/latest/libraries/Process.html
A. Running the .sh script in the same machine/os which you launch ROBOT Framework:
Run Process /appl/Test1/asd.sh shell=yes --option argument
B. Running the .sh script in another target machine;
In order to run .sh file, you must first write a ROBOT Framework's scripts to login to that unix/linux machine. So, follow the steps below.
Import SSH Library into your RIDE. Follow the steps here: http://robotframework.org/SSHLibrary/
Write ROBOT scripts to login to your unix/linux machine. Please go through all the available keywords under SSH Library first to get the hang of it. There are many ways to write the scripts but below are samples..
See sample login scripts below.
#SAMPLE A
Open Connection ${server_ip} prompt=$
${std_output}= Login ${username} ${password}
Should Contain ${std_output} $
#SAMPLE B
Write sudo -u root -i
${std_output}= Read delay=5s
Should Not Contain ${std_output} Permission denied
Should Not Contain ${std_output} Password:
#
Write cd /${hostname}_folder/Access
${std_output}= Read delay=5s
Log ${std_output}
Should Not Contain ${std_output} Permission denied
#
Write su devowner
${std_output}= Read delay=5s
Log ${std_output}
Should Not Contain ${std_output} Password:
Then, next you can use either WRITE or EXECUTE COMMAND keywords to run your .sh file at target machine/host.
On Unix based systems, the following should work:
Run Process /appl/Test1/asd.sh
On Windows, try
Run Process sh /appl/Test1/asd.sh
or
Run Process bash /appl/Test1/asd.sh

I cant use certain linux commands in a bash script altough I can use them in console

I am trying to write a bash script that utilizes the command mkvirtualenv.
I can use it in the console without a problem but as soons I as try to use it in a bash script I get ./run: line 1: mkvirtualenv: command not found
I am not aware of anything that would create such a situation.
Does anyone know why the bash script behaves like that?
The reason emerged form the comments below the question: mkvirtualenv is a function.
If you want the function to exist in the script, you can export it from your shell by
export -f mkvirtualenv

Automatically open terminal when running bash script

To get the script to run in terminal, I have to select the option to open in terminal and write sh script name.shIs there a way I can reduce that to a single step, i.e. a launcher that automatically opens the script in a terminal after logging as a root ? I've tried to look it up on Google, but I haven't found any useful advice (perhaps I'm not executing the search properly).
I think what you mean is running your script as start up script. In that case place the script you want to run in the /etc/init.d directory and make the script executable with command chmod 755 scriptname.sh.
See the below related threads for more information
https://askubuntu.com/questions/290099/how-to-run-a-script-during-boot-as-root
How to run a shell script at startup
EDIT:
if you want to run your script after your login is successful then you need to place your script in ~/.bash_profile. See this related post
How do you run a script on login in *nix?

How to change directory through a script file

i want to execute some commands through terminal. I have on script for executing commands.Some commands are working but when trying to change directory its not changing. There is no error while executing that script.The script which i made is executable and is mention below:
make clean
make
cd /home/user
save this as script.sh and make it executable
Current working directory is a process property. Each process has independent value for its working directory. Your script works correctly: it changes the current working directory of the shell process that executes it.
If you want your interactive shell to change working directory you have to instruct it. You can do it by "sourcing" your script into your interactive shell. "Sourcing" means reading the script and executing the commands by the shell that sources it. This is opposed to "executing" the script, where a separate shell process is started and executes the script contents.
You can source a script using source or . commands. Like this:
source script.sh
or this:
. script.sh

Resources