1 / 32

CSE4251 The Unix Programming Environment

CSE4251 The Unix Programming Environment. Lecture 2 Shell environment I: - command line interface & basic commands; directories & files ; standard I/O & file descriptors;. Recap. Shell a non-graphic user interface to access Unix-like OSes a process a command interpreter

ormand
Download Presentation

CSE4251 The Unix Programming Environment

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE4251 The Unix Programming Environment Lecture 2 Shell environment I: - command line interface & basic commands; directories & files; standard I/O & file descriptors;

  2. Recap • Shell • a non-graphic user interface to access Unix-like OSes • a process • a command interpreter • Log into shell • on Linux machine: • open terminal via GUI (normally) • on Windows machine: • run PuTTY(w/ Pageant) to connect to remote a Linux server • run Linux locally via VirtualBox/VMware-Workstation

  3. After you logged in ...

  4. After you logged in ... user name: zhengm

  5. After you logged in ... host machine name: eta

  6. After you logged in ... current directory: ~ (i.e., /home/zhengm)

  7. After you logged in ... bash command prompt: $

  8. Choose your shell • Check your current shell • $ echo $SHELL • $ echo $0 • Check availabe shells • $ cat /etc/shells • $chsh –l • Switch to another shell • $ chsh –s /bin/dash (changing to dash) • won’t take effect until next login • directly type the shell name e.g.: $ dash • temporarily change current shell without re-login (may not be supported on some OSes) $ cat /etc/shells /bin/sh /bin/bash /sbin/nologin /bin/tcsh /bin/csh /bin/dash

  9. Customize your shell • Every shell supports some customization • user prompt • shortcuts (alias) • paths • Customization takes place in startup files • configurations read by the shell automatically when it starts • system-wide config. file: /etc/profile (normally don’t touch) • your config. file: ~/.bashrc

  10. Customize your shell • Change user prompt • E.g., add the following line to your ~/.bashrc export PS1="[\t][\u@\H:\w]\r\n+ " \t : the current time in 24-hour HH:MM:SS format \u : the username of the current user \H : the hostname \r : carriage return \n : newline [zhengm@sl2 ~]$ [20:26:06][zhengm@sl2:~] +

  11. Customize your shell • Change user prompt • E.g., add the following line to your ~/.bashrc export PS1="[\t][\u@\H:\w]\r\n+ " \t : the current time in 24-hour HH:MM:SS format \u : the username of the current user \H : the hostname \r : carriage return \n : newline • More about customized alias and paths on later lectures [zhengm@sl2 ~]$ [20:26:06][zhengm@sl2:~] +

  12. Create account & change password • Create an username “joe” (usually in lower case) • $ useraddjoe • Sets an initial password for “joe” • $ passwdjoe • (optional) Create a group called “students” • $ groupaddstudents • (optional) Add “joe” to group “students” • $ useradd –G students joe • “joe” changes password after logged in using the initial password • $passwd

  13. Some commonly used commands • $ lssomedir# list files in dir • $ cdsomedir # enter some directory • $ uname –a # check your platform info • $mvoldfilenamenewfilename# rename a file • $ cpsrcfilenamedstfilename# copy a file • $ catfilename # display the content of a file • $ tail -n filename# display the last n lines of file • $ head -n filename # display the first n lines of file • $rmfilename # delete file/directory • $mkdirdirname# create a directory • $rmdirdirname # delete empty directory • $ mancmdname# lookup cmd usage

  14. What you see via “ls” is part of a file system • $ ls/

  15. File system 101 • A way to organize data/resource • “everything is a file” on Unix • Conceptually a tree structure

  16. The Structure of Directories • A file cc.txt in directory bb under directory aa under the root directory can be represented as /aa/bb/cc.txt • . means current directory $ cd . #still in current directory $ ./prog #run ‘prog’ in current dir • .. means parent directory $ cd .. #change to parent directory

  17. Several Important Directories • / • the root directory • /bin • executable/binary files • /dev • device files • /home • personal work directory, default place after logged in

  18. Several Important Directories • /etc • configuration files & some executable shell files • /usr • /usr/inlcude #header files • /usr/lib #libraries • /usr/bin/ #more executables • /sbin • executables which can only be executed by the super user (root)

  19. Some directory-related commands • $pwd • display current full directory path • $mkdirsomedirand $ rmdirsomedir • create and delete a directory • $ls • list the files in a directory • -l,-t • $cdsomedir • enter a directory

  20. Unix is multi-users/multi-tasks • Allow multiple users to log into a shell simultaneously • Allow multiple programs running concurrently • Need protection! • protect different users • protect different programs • Basic protection: access rights of file/directory

  21. Access rights for file/directory • Check access rights (permissions) • $ ls –l - rw- rw- r--. ... usenix.sty_.txt drwxrwxr-x. ... WinLog • d: directory

  22. Access rights for file/directory • Check access rights (permissions) • $ ls –l - rw- rw- r--. ... usenix.sty_.txt drwxrwxr-x. ... WinLog • access rights (permissions)

  23. Access Rights Groups • Three groups of Access rights • Owner (u) • The members within a group (g) • Others (o) - rw- rw- r--. ... usenix.sty_.txt drwxrwxr-x. ... WinLog • u • g • o

  24. Access rights representation • Each group has permissions represented by rwx or 0-7 • r mean read • w means write • x means executable • translated to 0-7 via binary values

  25. Access rights representation • What is permission 5? • 5 in binary format is : 101 5 = 101 = r--x Can read No write Can run

  26. Access rights representation • Three groups of access rights, three numbers • Owner (u) • The members within a group (g) • Others (o) = 7 6 4 = 111 110 100 = rwxrw-- r---- 764 owner can: read, write, run Others: can only read group can: read, write

  27. Commands related to the access right • chown: change the owner of a file • e.g., $ chownuser_namefile_name • chgrp: change the group of a file • e.g., $ chgrpgroup_namefile_name

  28. Commands related to the access right • chmod: change permissions for owner, group, or others • access groups: u, g, o, and a (all groups) • access rights: r, w, x. • e.g.: • $ chmodu+rwx, g+rx, o+rxfile_name • $ chmod 755 file_name • $ chmod o-x file_name

  29. Wildcards / Metacharactersfor filename abbreviation • Shell understands wildcards • Metacharacters: * ? [ ] ~ • * matches anything $ ls*.doc # list all files ending with .doc • ? matches single character $ ls?.doc # list a.doc, b.doc, but not ab.doc

  30. Wildcards / Metacharactersfor filename abbreviation • […] matches any of the enclosed charactors $ ls [ab].doc # only list a.doc or b.doc $ ls *[cx].doc # list files ending with c.doc or x.doc • ~ is a shortcut of your home directory $ cd ~ # change to home directory $ cd ~/music # change to /home/username/music directory

  31. Programs and Standard I/O Program • Standard Output (stdout) • Screen • Input to another program • A file • Standard Input (stdin) • Keyboard • Output from another program • A file Standard Error (stderr)

  32. File Descriptor • A file descriptor (fd) is an abstract indicator for accessing a file. It’s an Integer in Unix

More Related