bash作业 | 代写quiz | java | shell | lab – Quiz

Quiz

bash作业 | 代写quiz | shell | lab – 这是一个关于java/shell的题目, 主要考察了关于shell的内容,是一个比较经典的题目, 包括了bash/shell等方面, 该题目是值得借鉴的lab代写的题目

lab代写 代写lab

CISC 220, Section 1, Fall 2016

Question 1 (4 points):
Write a series of shell 代做 script代写”> bash commands that will:
1. create a new directory called Thursday (a sub-directory of the current directory)
2. create a file in that new directory called news.txt consisting of the single line There is
a quiz today!
Don’t try to write a command that will create the file with a text editor.
Question 2 (4 points):
Write a bash command that will copy all files in /cas/course/cisc220 whose names start with m
into your current directory. You may assume there will be no name conflicts (no files in
/cas/course/cisc220 with the same names as files in your current directory).
Question 3 (4 points):
You have a file called nochanges in your current directory. Write one or more bash commands that
will prevent anyone except you from being able to change the contents of this file but ensure that
anyone on the computer (including you) may read its contents. Your command(s) should not change
the permissions of any other files.
Question 4 (4 points):
Write the output of the following sequence of bash commands:
NUMBER=
OBJECT=computer
COLOR=red
echo My $OBJECT is $COLOR
echo I have $NUMBER $OBJECTs
For the purpose of this question, assume that NUMBER and OBJECT and COLOR are the only
variables defined in your shell (so the value of any other variable is the empty string).
Question 5 (4 points):
You have just created a shell script called myscript in your current directory. You would like to test
it with the arguments x and y. You type
myscript x y
to the command prompt and you get an error message:
no command ‘myscript’ found
Briefly explain why you might have gotten this error message and how you could change a single shell
variable so that “myscript x y ” would actually run myscript with arguments x and y.
Question 6 (4 points):
Read the following bash transcript:
——-$ kids 5 4
My brother is 5 years old
My sister is 4 years old
My brother is older
——-$ kids 2 6
My brother is 2 years old
My sister is 6 years old
My sister is older
——-$ kids 8 8
My brother is 8 years old
My sister is 8 years old
They are twins
Write a kids script that would behave as shown above.
Question 7 (4 points):
Read the following bash transcript:
——-$ friend Timothy
My friend is named Timothy
His nickname is Tim
——-$ friend Robert
My friend is named Robert
His nickname is Rob
Write a friend script that would behave as shown above.
Question 8 ( 4 points):
Write a script called squares which prints the square of each of its command-line arguments. You may
assume that all of the arguments will be integers. If there are no command-line arguments the script must print
nothing.
For example:
——-$ squares 5 2 7
25
4
49
This page has intentionally been left blank. You may use it for rough work. If you write an answer
here, be sure to indicate that on the question page so we’ll know to look here.

Summary Page: Basic Linux & Bash

CISC 220, Fall 2016, Section 1
(last update: 2 September 2016)

Make sure you’re running bash: Right after you log in, type this command: ps (This command lists all the processes you’re currently running. Don’t worry about the details for now.) If you’re running bash, the output will include a process called bash. If not, send an e-mail to Margaret ([email protected]) to get your login shell changed to bash. For now, you can type bash to switch to that shell.

Navigating directories: cd ( change directory): moves you to another directory cd otherDir : moves you to otherDir cd with no arguments: moves you back to your home directory pwd ( print working directory): shows the name of your current directory

Listing file information: the ls command arguments should be names of files & directories for each file: lists the file for each directory: lists the contents of the directory (unless d flag) ls with no arguments: equivalent to ls. (list the current directory) some flags for ls : -a : include files & directories starting with “.” -d : for directories, show directory itself instead of contents -l : (lower-case L) long format: lots of information about each entry -R : list sub-directories recursively -1 : (one) list each file on separate line (no columns)

Displaying the contents of a short file: cat

Reading a longer file: less While running the less program, use these single-character commands to navigate through the file: A. f or space: forward one window B. b: backward one window C. e or return: forward one line D. y: backward one line E. h: display help screen which describes more commands F. q: exit

Wildcards in file names: ?: any single character *: any sequence of zero or more characters

Information about commands (Linux “manual”): man ls : information about the ls command

File/directory protections: chmod = is u for owner, g for group, o for other users is r for read, w for write, x for execute can use + or instead of =, to add or subtract permissions umask = : sets the default protections for new files you create umask S : displays your current set of default protections in symbolic (not binary) form

Examples of File/directory protection commands: chmod g+rx myprogram : gives group members read permission and execute for myprogram chmod u+w * : gives owner write permission to all files in current directory Example using umask : ——-$ umask -S u=rwx,g=,o= ——-$ umask g+rx ——-$ umask -S u=rwx,g=rx,o= ——-$ umask g-x ——-$ umask -S u=rwx,g=r,o= ——-$ umask g+w ——-$ umask -S u=rwx,g=rw,o= ——-$ umask g= ——-$ umask -S u=rwx,g=,o= ——-$

Copying files: cp oldFile newFile oldFile must be an existing file. Makes a copy and calls it newFile. cp file1 file2 file3… fileN dir file1 fileN must be existing files and dir must be a directory. Puts copies of files in directory dir

Moving/renaming files: mv oldFile newFile mv file1 file2 file3… fileN dir This command command is similar to copy , but it gives files new names and/or locations instead of making extra copies. After this command the old file names will be gone.

Deleting files: rm CAREFUL — no recycle bin or un-delete! rm -i interactive mode, asks for confirmation rm f suppresses error message if files don’t exist

Creating & deleting directories: mkdir dir creates new directory called dir rmdir dir deletes dir , providing it is empty rm r dir removes dir and all of the files and sub-directories inside it use with great caution!!!

Create a file or change a timestamp: touch filename If filename exists, changes its last access & modification times to the present time. If not, creates an empty file with that name.

Log out of the Linux system: logout

End current shell: exit

See a list of your current jobs: jobs

Run a command in the background: &

Change to a background job: %n , where n is number of job as shown by jobs %pre , where pre is a prefix of the job name

To terminate a job: kill %n or kill %pre

To stop a foreground job: control-c (hold down the control key and type c)

Text editors: emacs : Start emacs with the emacs command. To go to a tutorial, type control-h followed by “t”. To exit emacs, type control-x following by control-c. To suspend emacs (put it in the background), type control- x following by control-z. vim : The shell command vimtutor will start a tutorial to teach you how to use vim. To exit vim , type :q!. If that doesn’t work, you’re probably in “insert mode”, so type Escape to go back to “edit mode” and try again. To start vim without the tutor, just use the vim command. nano : a scaled-down version of emacs, with a menu showing to help you get started There are other editors on CAS lab Linux as well, but most use graphics (not just plain characters) so they don’t work in a shell window, which means you can’t use them over putty or in a Vagrant window.

Summary Page: More Shell Skills

CISC 220, Fall 2016, Section 1
(last update 9/22/2016)
Sub-Shells
bash (starts up a sub-shell, running bash)
exit (exits from the current shell back to parent or logs out if this is login shell)
Shell Scripts
script = file containing list of bash commands
comments start with “#” (to end of line)
$0 is name of command
$1 , $2 , … are the command-line arguments
$# is number of command-line arguments (not counting $0)
$* is all command-line arguments in one string, separated by spaces
to execute script in a sub-shell, type file name of script
to execute script in current shell: source + name of script
Shell Variables
today=Tuesday
(sets value of today to “Tuesday” ; creates variable if not previously defined)
(important: no spaces on either side of equals sign)
set (displays all current variables & their values)
echo $today
(displays value of today variable; output should be “Tuesday” )
export today
(sets property of today variable so it is exported to sub-shells)
Echo & Quoting
echo (prints its arguments to the standard output)
echo n (doesn’t start a new line at the end useful for prompts in interactive scripts)
backslash () protects literal value of the following character
single quotes protect the literal value of every character
double quotes protect the literal value of every character with a few exceptions:
dollar sign ($), back quote (`), and exclamation point (!)
examples:
——$ today=Tuesday
——$ echo Today is $today
Today is Tuesday
——$ echo “Today is $today”
Today is Tuesday
——$ echo ‘Today is $today’
Today is $today
——$ echo “${today}s child is fair of face.”
(Note the braces in the line above they tell the shell that the s is
not part of the variable name.)
Tuesdays child is fair of face.
——$ today=”$today Jan 13″
——$ echo $today
Tuesday Jan 13
(example continued on next page)
——$ today=”${today}, 2009″
——$ echo $today
Tuesday Jan 13, 2009
——$ echo the price is $2.
the price is $2.
Useful Predefined Shell Variables:
$PS1 : your shell prompt. May include special values:
\d : the current date
\h : the name of the host machine
\j : number of jobs you have running
\s : the name of the shell
\w : current working directory
! : history number of this command
note: these special character sequences only work as part of $PS
$HOME : your home directory (same as ~)
$ PATH : list of directories in which to find programs directory names separated by colons
$PS2 : secondary prompt for multi-line commands
$? : the exit status of the last command (0 means successful completetion, non-zero means
failure)
$SHLVL : shell level (1 for top-level terminal, larger for sub-shells)
Initialization Files
When you log onto Linux (directly to a shell), it executes ~/.bash_profile
When you start a sub-shell, Linux executes ~/.bashrc
Redirection & Pipes
cmd < inputFile (runs cmd taking input from inputFile instead of keyboard)
cmd > outputFile (sends normal output to outputFile instead of screen)
cmd >| outputFile (if outputFile already exists, overwrites it)
cmd >> outputFile (if outputFile already exists, appends to it)
cmd 2> errFile (sends error messages to errFile instead of screen)
cmd 1>outFile 2>&1 (sends both normal and error output to outFile)
cmd 2>outFile 1>&2 (does same as previous)
cmdA | cmdB (a pipe: output from cmdA is input to cmdB)
Special file name for output: /dev/null. Text sent here is thrown away.
Aliases
alias newcmd=”ls l” (typing newcmd is now equivalent to typing
ls l )
unalias newcmd (removes alias for newcmd )
alias rm=”rm i” (automatically get i option with rm command)
‘rm’ or “rm” (the original rm command, without the alias)
Summary Page: Shell Scripting
CISC 220
(last update 8. October 2017)

Links ln file1 file file1 should be an existing file. This command creates a “hard link” to file1, called file2. You can’t create hard links to a file on a different physical device or to a directory. ln s file1 file file2 becomes a symbolic link to file1 a special file containing the name “file1”. No restrictions.

Exit Status: Every bash command has an exit status: 0 = success, non-zero = failure

exit Command exit n ends a script immediately, returning n as its exit status

Useful commands: Getting Parts of Filenames dirname filename prints the folder part of filename minus base name basename filename prints filename without its folder basename filename suffix prints filename without its folder and suffix (if suffix matches) Examples: ——$ basename /cas/course/cisc220/pride.txt pride.txt ——$ basename /cas/course/cisc220/pride.txt .txt pride ——$ basename /cas/course/cisc220/pride.txt .cpp pride.txt ——$ dirname /cas/course/cisc220/pride.txt /cas/course/cisc

Command Substitution When you write $(cmd) , Bash runs cmd and substitutes its output. Example: ——$ FILENAME=/cas/course/cisc220/somefile.txt ——$ DIR= $(dirname $FILENAME) … $DIR is now /cas/course/cisc

Conditional Statements: [[ expression ]] : evaluates expression using string comparisons not arithmetic! You must include a space after “[[” and before “]]”! You must put “$” before variable names. Statement succeeds (exit status 0) if the condition is true. No output; just used for exit status. May use string comparisons with “=”, “!=”, “<” and “>”;. There are no <= or >= operators. File query operators: -e file : file exists -d file : file exists and is a directory -f file : file exists and is a regular file -h file : file exists and is a symbolic link -r file : file exists and is readable -s file : file exists and has size > 0 -w file : file exists and is writeable -x file : file exists and is executable file1 nt file2 : file1 is newer than file

Arithmetic Conditional Statements: ((expression)) The expression can contain assignments and arithmetic with syntax like java or C. No need to use “$” before variable names ((X > Y+1)) ((Y = X + 14)) ((X++)) The expression succeeds (exit status 0) if the value of the expression is true or a non-zero number. Operators: +, -, *, /, %, ++ , – , !, && , ||

Arithmetic Substitution: ——-$ sum=$((5+7)) ——-$ echo $sum 12

If Command if test-commands ; then consequent-commands ; [elif more-test-commands ; then more-consequents ;] [else alternate-consequents ;] fi Example: if [[ $X == $Y ]] then echo “equal” else echo “not equal” fi

While Command while test-commands ; do consequent-commands ; done Example while ((X<=4)) do echo $X ((X++)) done

For Command for name [in words …]; do commands ; done Example: for ARG in $* do ls $ARG done

Useful command with for loops: seq x y prints all integers from x to y Example: for X in $(seq 1 10) do ((SUM = SUM + X)) done

shift Command shift “shifts” arguments to the left Example: if command-line arguments are “a” , “b” and “c” initially: after shift command they are “b” and “c” (and $# becomes 2)

User Interaction During a Shell Script read myvar Waits for user to type a line of input and assigns the input line to variable myvar. read var1 var2 Same as above, but assigns the first word of the input to var1 and the rest of the line to var2. May be extended to as many variables as you want. read p prompt Types the prompt before waiting for input echo n …. Same as normal echo, but doesn’t add a newline character. Useful for prompts.

Shell Variables: Advanced Features ${var:-alt} expands to alt if var is unset or an empty string; otherwise, expands to value of var

${var:offset:length} Expands to a substring of var. I ndexes start at 0. If length is omitted, goes to end of string. Negative offset starts from end of string. You must have a space between the colon and the negative sign! ${#var} expands to the length of $var

${var/pattern/string} expands to the value of $var with the first match of pattern replaced by string.

${var//pattern/string} expands to the value of $var with all matches of pattern replaced by string.