In my shell script I would like to list all the files in my current directory - linux

In my shell script I would like to list all the files and directories in my current directory.
I know the command is ls, but I have no idea how to run it in a shellscript.
Thanks for anyhelp.

So you're basically asking: what is a shell-script, how do I create one, and how do I run it ....
Use your editor of choice to create a file, give it the following content:
#!/bin/sh # change to your preferred shell, sh being a low common denominator
command # in your immediate question that would be `ls`
Save the file.
Run chmod u+x file (not the word file, but what you called your saved script).
Then you can execute your file like so:
./file

Related

How do you run bash script as a command?

I have a bash script, which I use for configuration of different parameters in text files in my wireless access media server.
The script is located in one directory, and because I do all of configurations using putty, I have to either use the full path of the file or move to the directory that contains the file. I would like to avoid this.
Is it possible to save the bash script in or edit the bash script so that I can run it as command, for example as cp or ls commands?
The script needs to be executable, with:
chmod +x scriptname
(or similar).
Also, you want the script to be located in a directory that is in your PATH.
To see your PATH use:
echo $PATH
Your choices are: to move (or link) the file into one of those directories, or to add the directory it is in to your PATH.
You can add a directory to your PATH with:
PATH=$PATH:/name/of/my/directory
and if you do this in the file $HOME/.bashrc it will happen for each of your shell's automatically.
You can place a softlink to the script under /usr/local/bin (Should be in $PATH like John said)
ln -s /path/to/script /usr/local/bin/scriptname
This should do the trick.
You can write a minimal wrapper in your home directory:
#!/bin/bash
exec /yourpath/yourfile.extension
And run your child script with this command ./NameOfYourScript
update: Unix hawks will probably say the first solution is a no-brainer because of the additional admin work it will load on you. Agreed, but on your requirements, my solution works :)
Otherwise, you can use an alias; you will have to amend your .bashrc
alias menu='bash /yourpath/menuScript.sh'
Another way is to run it with:
/bin/bash /path/to/script
Then the file doesn't need to be executable.

Add a bash script to path

I want to add a small script to the linux PATH so I don't have to actually run it where it's physically placed on disk.
The script is quite simple is about giving apt-get access through a proxy I made it like this:
#!/bin/bash
array=( $# )
len=${#array[#]}
_args=${array[#]:1:$len}
sudo http_proxy="http://user:password#server:port" apt-get $_args
Then I saved this as apt-proxy.sh, set it to +x (chmod) and everything is working fine when I am in the directory where this file is placed.
My question is : how to add this apt-proxy to PATH so I can actually call it as if it where the real apt-get ? [from anywhere]
Looking for command line only solutions, if you know how to do by GUI its nice, but not what I am looking for.
Try this:
Save the script as apt-proxy (without the .sh extension) in some directory, like ~/bin.
Add ~/bin to your PATH, typing export PATH=$PATH:~/bin
If you need it permanently, add that last line in your ~/.bashrc. If you're using zsh, then add it to ~/.zshrc instead.
Then you can just run apt-proxy with your arguments and it will run anywhere.
Note that if you export the PATH variable in a specific window it won't update in other bash instances.
You want to define that directory to the path variable, not the actual binary e.g.
PATH=$MYDIR:$PATH
where MYDIR is defined as the directory containing your binary e.g.
PATH=/Users/username/bin:$PATH
You should put this in your startup script e.g. .bashrc such that it runs each time a shell process is invoked.
Note that order is important, and the PATH is evaluated such that if a script matching your name is found in an earlier entry in the path variable, then that's the one you'll execute. So you could name your script as apt-get and put it earlier in the path. I wouldn't do that since it's confusing. You may want to investigate shell aliases instead.
I note also that you say it works fine from your current directory. If by that you mean you have the current directory in your path (.) then that's a potential security risk. Someone could put some trojan variant of a common utility (e.g. ls) in a directory, then get you to cd to that directory and run it inadvertently.
As a final step, after following the solution form proposed by #jlhonora (https://stackoverflow.com/a/20054809/6311511), change the permissions of the files in the folder "~/bin". You can use this:
chmod -R 755 ~/bin
make an alias to the executable into the ~/.bash_profile file and then use it from anywhere or you can source the directory containing the executables you need run from anywhere and that will do the trick for you.
adding to #jlhonora
your changes in ~./bashrc or ~./zshrc won't reflect until you do
source ~./zshrc or source ./bashrc , or restart your pc

Run bash script as if I it was executed from a different directory

Maybe the title is a bit "stupid" but I do not know how to express my question and how to search for the question also, even if it is something very simple.
I have a set of scripts that produce a set of reports in the folder they are executed by. For example I have the script "my_script.sh" in the folder /a/folder/ and in this folder a set of output is stored. Since I have a lot of experiments that I want to let them run for the whole week I was thinking of creating a bash script that will call all the other scripts.
But the output will be stored in the folder that the global script is present.
For example:
/global/folder/global_script.sh
---> All the output is stored in this folder.
The global_script.sh may contain something like this:
/experiments/exp1/script1.sh >report1.txt
/experiments/exp1/script2.sh >report2.txt
/experiments/exp1/script2.sh >report3.txt
And I want the output of the bash scripts to be in their folder and not in the global folder.
Currently I am doing this manually navigating to the folder and executing the script.
(Ok I can change the code and use absolute paths! but is any better way to do that? )
you could change the working directory before you execute each script, or redirect the output to the directory you want:
cd /experiments/exp1/
sh /experiments/exp1/script1.sh >report1.txt
or
sh /experiments/exp1/script1.sh > /experiments/exp1/report1.txt
What's wrong with simply changing directory?
cd /experiments/exp1
./script1.sh >report1.txt
./script2.sh >report2.txt
./script2.sh >report3.txt

alias - cd followed by ls

How can I define an alias so that when I do cd Abcd, where 'Abcd' is the name of a directory, the directory is changed to 'Abcd' and is followed by ls to show the contents of the directory?
I believe you can't use an alias to accomplish that, but you could define a function to do it:
#print contents after moving to given directory
cl()
{
cd $#
ls
}
You could stick this in your ~/.bashrc file.
If you were hoping to override the builtin cd command, then you could do:
#print contents after moving to given directory
cd()
{
builtin cd $#
ls
}
UNIX
Creating an alias
Your Linux distribution will most likely not have the .bash_aliases file created in your home, or you can even create it manually. To create the file, type in the following command:
touch ~/.bash_alisaes
Now that file will be executed automatically every time you fire off a new Terminal.
What you can do now is create a list of aliases and add them to that file for later uses.
create an alias and update the ~/.bash_aliases file to make it permanent.
Generic approach: Creating a custom script
Create a bash script in your /usr/bin folder, it should look something like this
#!/bin/bash
Whatever combination of commands you want to run when you type this thing.
Its really that easy.
Just name the bash script what you want to type in to the terminal, and make it excecutable: chmod +x filename and you're good to go!
WINDOWS
You can use DOSKEY command:
From Wikipedia:
DOSKey is a utility for MS-DOS and Microsoft Windows that adds command
history, macro functionality, and improved editing features to the
command line interpretersCOMMAND.COM and cmd.exe. It was included as a
TSR program with MS-DOS and PC-DOS versions 5 and later, and with
Microsoft's Windows 95/98/Me.
For example: To create a macro that quickly and unconditionally formats a disk, type:
doskey qf=format $1 /q /u
To quickly and unconditionally format a disk in drive Z, type:
qf Z:
To define a macro with multiple commands, use $t to separate commands, so the solution to your problem follows:
doskey cd=cd $1$tdir
Now, this will work only in your currently open command window. To make it permanent simply create a batch file and set the value of the absolute path of the file to the regedit
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
Source for the regedit: superuser.com/a/238858

simple shell script to copy files and folders and also execute a command

I haven't written any Shell scripts before, but i have to write a simple shell script to do the following;
I will keep all the required files in a single folder and bundle it with this shell script as a tar file; so when the user runs the shell script, it needs to copy the respective files to the respective destinations.
The execution of copy as follows:
copy the plugin.so file to /usrlib/mozilla/plugins/
copy the .so library files to /usr/local/lib/
copy some header files directories(folders) to /usr/local/include/
and finally, need to do ldconfig.
Basically, you can add in a script any command you are able to type inside the terminal itself. Then, you have two options for executing it:
Execute it from the terminal with sh your_script.sh. You don't even need to give execute permission to it with this solution.
Give it the execute permission and run it with ./your_script.sh.
For the second solution, you have to start the file with what is called a shebang. So your script will look like:
#!/bin/sh
cp path/to/source path/to/destination
cp path/to/source path/to/destination
cp path/to/source path/to/destination
ldconfig
echo "Done!"
Nothing else. Just write the commands one after the other.
The first line is the so-called shebang and tells the shell which interpreter to use for the script.
Note: the extension for shell scripts is usually .sh, but you can actually name your file however you prefer. The extension has no meaning at all.
Good scripting!

Resources