Brief overview of I/O, file permission bits, stdin/stdout/stderr and redirection, bash pipelines Section 1 Unix I/O Stream versus file descriptor When a program opens a file it is returned a reference to a stream FILE *fopen(const char *path, const char *mode); There are different types of streams with different properties. For example, with files, this is a buffered stream which allows our program to position where we want to read from - we can read line by line, we can move forward in position of the sequential stream of octets or we can move backwords. To obtain the descriptor from a stream: int fileno(FILE *stream); Section 2 file access control bits Issue 'ls -alt' to see a long listing of all files in the current directory. The information starting at the left of each file represents the file 'permission' bits. In the original Unix (from early 1970's to current Linux distros) the basic mechanism Unix uses for ensuring users can only see and do that is permitted is by assuming three categories of users: user: the owner of the file group: a set of users can be placed into a group. Groups can be given access to files world: What any user can access Unix maintains rwx (read, write, execute) permissions for access to I/O (which indirectly controls who can run what programs). Therefore, each category of user can have different rwx perimissions. On my system, an 'ls -lt somefile.txt' shows: rw-rw-r-- 1 jjm jjm 0 Feb 14 15:15 somefile.txt The output fields can be broken down into 4 fields: < owner group > < last update time > Example: access bits: 10 bits ' rw-rw-r--' The most significant bit (left most0 shows blank) which means it is a 'regular file' It would be a 'd' if the file were actually a directory We read then next 9 bits in groups of 3 and form an octal number: bits 987 : rw- 110 (6) bits 654 : rw- 110 (6) bits 321 : r-- 100 (4) The 10th bit (the furthest to the left) specifies the type of file. A regular file will be blank. Special files will be shown with a character: 'd' : directory 'p' : pipe 's' : symbolic link 'f' : regular file (usually not shown) Then there are three groups of 3 bits. The three bits form an octal number : 0-7 access bits 876 form the rwx permissio bits of the owner of the file (listed after the access bits) 543 form the rwx permission bits for users in the group 210 form the rwx permission bits for all others owner and group : identifies a valid user id on the system who owns the file. Users can be placed into groups - file access can be controlled based on what group name is associated with the file. last update time : the time the file last changed filename: And the filename. Example, ls -lt readerPipe.sh -rwxr-xr-x 1 jjm jjm 482 Feb 16 18:02 readerPipe.sh -: not a special file rwx: owner of the file has read write and execute access r-x : the group has read and execute access r-x : the world has read and execute access The chmod command allows users to change permission bits. ----first page from 'man chmod' ----------------------- CHMOD(1) User Commands CHMOD(1) NAME chmod - change file mode bits SYNOPSIS chmod [OPTION]... MODE[,MODE]... FILE... chmod [OPTION]... OCTAL-MODE FILE... chmod [OPTION]... --reference=RFILE FILE... DESCRIPTION This manual page documents the GNU version of chmod. chmod changes the file mode bits of each given file according to mode, which can be either a symbolic representation of changes to make, or an octal number representing the bit pattern for the new mode bits. The format of a symbolic mode is [ugoa...][[-+=][perms...]...], where perms is either zero or more letters from the set rwxXst, or a single letter from the set ugo. Multiple symbolic modes can be given, separated by commas. A combination of the letters ugoa controls which users' access to the file will be changed: the user who owns it (u), other users in the file's group (g), other users not in the file's group (o), or all users (a). If none of these are given, the effect is as if (a) were given, but bits that are set in the umask are not affected. The operator + causes the selected file mode bits to be added to the existing file mode bits of each file; - causes them to be removed; and = causes them to be added and causes unmentioned bits to be removed except that a directory's unmentioned set user and group ID bits are not affected. --------------------- To allow the group write access: chmod 775 To change the file to be rwx only by the owner, and just readable to group users and the world: chmod 744 readerPipe.sh Each digit is in octal: it sets the 3 bits for the owner, group, world 7: in binary is 111 which maps to rwx 4: in binary is 100 which maps to r-- As we have learned, there is a highest level of sys admin users called 'super user' or root. On a system under your control, issuea 'sudo su -l ' and if you have sysadmin access you enter your password and become root. Most Linux systems by default do not allow the following (even if you know root's password): 'su -l' (the su command is 'switch user', e.g., if you create a test user account, you could 'su -l testuser' ) It's possible to setup ubuntu to allow this.... but I would suggest NEVER to do anything as root. One mistake can wipe out the system. The 'sudo' command provides a flexible way to temporarily elevate a user to root access for the duration of the command. If your user account is a 'sys admin' then you can access everthing through sudo. On most production systems, a sys admin limits users to the minimum number of files and programs needed by editing /etc/sudoers. To change the owner and group of the file to user id trudy sudo chown trudy:trudy readerPipe.sh Section 3 stdout, stdin, stderr and redirection By default, every process is provided with access to three streams: stdin, stdout and stderr. The convention is for these streams to map to descriptors 0,1,2 respectively. The 'standard out' represents the output display assigned to the shell. This typically involves use of the ancient but still used 'tty' subsystem. Issue 'ls /dev/tty*' The tty's with numbers are active tty sessions To see who/what is using specific tty's, issue 'ps aux' and look at the tty column. To find mine, is issue 'ps aux | grep jjm' and I see each of my open terms (bash) as a pts/x where x is a number. The last process is the grep and it shows pts/2. Console terms use the pseudo terminal subsystem...just like tty, pts is an ancient subsystem that is still used. I can issue 'echo "hello" > /dev/pts/2 ' and the word hello would appear. I can redirect my standard output to any pts that is owned by my userid. I can redirect standard out to 'the bit bucket' if we are not interested in ever seeing the output: echo "hello" > /dev/null Redirection means we can redirect stdout or stderr to another output stream. Similarly, we can redirect stdin to come from an alternative input stream. Try the following: #This equivalent to 'cat fileData.dat' cat < fileData.dat #redirects stdout to the file ~/tmp/tmp.out cat fileData.dat > ~/tmp/tmp.out #redirects stdout to the 'bit bucket' - this is a device that accepts input streams and simple discards everything it reads. cat fileData.dat > /dev/null #equivalent to 'cat fileData.dat' cat fileData.dat > /dev/stdout This is one way to redirect stderr to stdout WARNING: stdout must be redirected first Then stderr is correctly redirected to the same cat fileData.dat 1> ~/tmp/tmp.out 2>&1 Try the following cp fileData.dat badDataFile.dat chmod 000 badDataFile.dat ls -lt ---------- 1 jjm jjm 314541 Feb 19 10:35 badDataFile.dat #If we try to read the file and send to stdout we get an error sent to stderr cat badDataFile.dat cat: badDataFile.dat: Permission denied #This allows tmp.out to hold stderr or stdout cat badDataFile.dat 1> ~/tmp/tmp.out 2>&1 Section 3 pipes in bash When we issue a 'cat fileData.dat' Bash usess the popen() system call to fork a child to run the cat operation and the call sets up a pipe between the child and parent so that the stdout from the child is read and displayed by the parent. Issue a 'cat fileData.dat | wc' This is a 'pipeline' in bash. The standard out of the first command is piped into the stdin of the second command. The bash shell forks two child processes to run the two commands. It sets up a pipe between child 1 such that its stdout is piped to child 2's stdin.