Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

run a single script as root user

The following text is take from http://askubuntu.com/questions/167847/how-to-run-bash-script-as-root-with-no-password
There is a very neat trick in every Linux which will allow you to do so. It is called the SetUI bit.
Keep in mind that you will need to have the permissions locked down tight in this file for this to be secure.
Make the file owned by root and group root:
sudo chown root.root <my script>
Now set the sticky but, make it executable for all and writable only by root:
sudo chmod 4755 <my script>
Keep in mind if this script will allow any input or editing of files, this will also be done as root.
The SetUID bit makes a script or binary always run as the owner of the file/binary, an example of such a binary is 'passwd'.
There is a solution using sudoers here is an example you could use:
Cmnd_Alias        CMDS = /path/to/your/script

<username>  ALL=NOPASSWD: CMDS
Now just place sudo in front of your script and it should run without asking for a password.

make file

Here's a great tutorial on writing a make file, http://mrbook.org/tutorials/make/

Intel Compiler

./configure CC=icc CXX=icpc F77=ifort FC=ifort

screen

I like to use the screen command to get a persistent shell window and I found a really useful .screenrc file that makes it easier to execute screen commands and see which windows are open.  The screen commands are executed following the ` command.  Here is the contents of the .screenrc file

escape ``
hardstatus alwayslastline
hardstatus string '%{= wk}%-Lw%{= KW}%50>%n%f* %t%{= dK}%+Lw%<'

rsync

'rsync' is a very convenient command to sync files between two directories, even on different computers or networks.  Here's a link to a tutorial.

ssh keys

Here's a good article on how to set up ssh keys (link).

ssh config

I recently discovered a convenient shortcut for ssh hosts which works on a Mac but I expect it should work on any *nix system.  In the ~/.ssh folder you can create a file called config and add the following lines:
Host nickname
Hostname ipaddress.or.hostname
User username
Then when you want to ssh to that server you only have to type
ssh nickname
and the remaining information is automatically added.

sed

sed is a great command line tool for *nix operating systems.  There's a great tutorial on it here.

I definitely don't use it to it's full potential but it's convenient for stripping parts of a file out.  Say you want to get a part between two key words, use this
sed -e '1,/StartKeyword/ d' -e '/EndKeyword/,$ d' in.txt > out.txt
This command says to delete everything from line 1 to 'StartKeyword' and then to delete everything from 'EndKeyword' to the end of the file '$' and to put it into out.txt.

Alternatively, you can use the ! (not) sign to do the opposite.  For example, say I want to keep everything between lines 1-10 and put that into a file, use this
sed -e '1,10 !d' in.txt > out.txt
So, sed becomes quite convenient for working with large text files, and I haven't even begun to scratch the surface of what it can do.

bash script

Occasionally I need to write bash scripts and just wanted to jot down some notes that are helpful when doing that.

The first line of the script should read
 #!/bin/bash

And then you can check the number of command line arguments that are passed into the script with
$#
and then the arguments can be accessed by using
$1 or $2, etc.
and the name of the script can be accessed with
$0
and finally, all the command line arguments can be accessed with
$@