chmod — Usage : For changing file security.
Background: User permissions are shown in Unix (using ls -l) as a 10 char string
1. The first character signify file type [‘-‘ here signifies file]
2. Next three tells about user’s permission [rwx here means read write and execute]
3. Next three are for permissions of member with same group as user [r– here means users group has read and write but NO execute]
4. Next is for all the other members. [r– signifies only read permissions]
Permissions can be changed in two ways using chmod
1. chmod [g/u/o] [+/- ][r/w/x]
Here g is for “group”, u is for “user”, o is for “other”, + is for adding and – is for removing, r(read), w(write), x(eXecute) permissions.
2. the other mode is more interesting and a bit confusing for beginners. It’s called absolute mode. There are two things to keep in mind here
a. Weight of r/w/x
b. Order of various categories or users
Weight is r –> 4 w–> 2 and x–>1
Order is fixed as u(User)g(Group)o(Other)
chmod 755 <filename> means
“give u(7 = 4 + 2 + 1 => read+write+execute) g(5= 4 + 1 means read+execute)o(same as group) permissions for the file with name <filename>”
Common files that are being used while writing shell scripts
1. /dev/tty –> points to the default terminal via which the shell script is running
2. /dev/null –> used when you don’t care about the output.
Standard I/O , Standard Error and Redirection
Unix takes input from Standard Input , prints back to Standard output and throws error to Standard Error and unless specified otherwise all points to the terminal. With these assumptions it has to be very flexible in changing these three to create real world scripts. This is done via “pipes” and redirections.
“>” operator makes the file following it the new standard output
“<” operator makes the file following it the new standard input.
“&>” for redirecting standard error in csh.
The next feature is pipe, which redirects “flow” from one command to another
The general syntax for using pipe is :
command [Arguments] | nextCommand | nextCommandAgain
so the output from the first command becomes the input to the next command which in turns can generate input to some other command.
Sometimes its desirable that the output from one particular subroutine flows to more than one place, this problem could be handled via Tee
Syntax would look something like
<command1> [Arguments] Tee command2 | command3
so Tee would pass the output of “command1” to both “command2” and “command3”
Export command “To create shell variables “persistent” ”
In real world the files are deep inside some directory structure example “\home\tanmay\dev\quanlib\core\maths\numerics\pdesolver.hs”
Would be cool if I can alias such long path or even better automate the whole changing directory to the level by calling say “goNumerics”
Also some cool applications/ ide uses some global variable to provide including or extra directories. [Erlang uses ERL_LIBS], then it’s probably a good idea to declare them always.
That’s where export command helps.
exporting in your “.profile” or “.bash” or “.bash_rc” file would make sure that whenever you start a new “bash” shell you get all these cool stuff/variable available for you.
Some important command-line characters
1. Background process (‘&’) –> This is one of the most used (by me ) character. Some applications (emacs for example) would always keep itself attached to the terminal in which it is running. Which makes the terminal unusable for other activities unless you exit emacs. Which is annoying if you are in deep directory/file and might want to run some some commands after some edits (compile for example).So solution is to start emacs with an ampersand(‘&’) which would makes it run in background
2. Command Grouping [()] –> you want the first line of you file to be current timestamp and then following by the output of you shell script one way is to write it like
date >> output.txt
mycommand [args] >> output.txt
other way is to use command grouping as (date; mycommand [args]) >>output.txt
btw ‘>>’ is used for append rather then delete and create the file on the right side.
While automating your build, or may be some other task that you achieve using the shell script, there are two kind of actions that you would probably want to do. (In my opinion these two actions are exhaustive)
1. Run <command1> and then after it runs successfully run <command2> and so on
2. Run <command1> and then run <command2> and so on.
These two effects can be achieved by using conditional executions
&& for 1st case
|| for the second.
<command1> && <command 2>
<command1> || <command 2>