CSE 390A, Spring 2014 Assignment 5: Basic Shell Scripting

本文档介绍了一个针对初学者的Bash shell脚本任务,包括登录脚本配置、消息打印、多行文本重复输出及文件统计信息等功能实现。

This assignment focuses on Bash shell scripting at an introductory level.  Electronically turn in THREE files: .bash_profile, mantra.sh, and filestats.sh as described in this document.  To receive credit, your .bash_profile should also be present in your home directory. 

Task 1: Login script, .bash_profile (revisited) 

As we have discussed, .bash_profile is a script that runs every time you log in to a Bash shell.  For this part of this assignment, add to your previous .bash_profile file in your home directory to perform the following new operations: 

1. (Self-Discovery) Use the mesg command to set whether or not you want to be able to be contacted by commands such as write and wall.  Either setting (yes or no) is fine; but explicitly set one or the other. 

2. Set the user's prompt to consist of the current username, shortened host name, and working directory, with separators between each.  For example, if user hank is logged into host attu3.cs.washington.edu and is in directory /usr/bin, the prompt would be: hank@attu3:/usr/bin$ 
Use appropriate commands and/or environment variables to discover each piece of info (Who is the current user? What host are they on?) to insert into the prompt.   A tricky part is inserting the current directory; you can't do this by just running the pwd command, because it won't update each time the user changes to a new directory.  Instead, insert the special symbol \w into your prompt text to tell Bash to put the working directory into the prompt. 

3. At the end of the file, output this message to the user as he/she logs in.  For user hank, the message might be: 

Hello, hank! Your shell is /bin/bash. The current time is 01:43 PM, Monday April 28, 2014. 
To be clear, you are not supposed to literally output hank and exactly that shell, and date/time shown above.  You are to use appropriate commands to discover the current user's username and shell (there are shells other than bash that someone could be running) and to display the current time in the format shown.  Read the man pages about displaying the current date/time with an appropriate "+FORMAT" parameter. For example, to show a date such as 04/28, you could write: date "+%m/%d" 
(Self-Discovery) If you want an optional extra challenge worth 0.001 points of extra credit, change the first output line above to print the user's real name rather than username. Hello, Hank Levy! 
To do this, you would need to perform a lookup on the person's real name based on their username.  You can do this by examining the contents of the /etc/passwd file for the given username.  There should be a line like this: hank:x:1000:1000:Hank Levy,,,:/home/hank:/bin/bash 

Notice that the various information about the user is separated by colon : characters.  The cut command can chop a line into fields based on a delimiter and select only certain field(s) to output.  You can use it twice: once to separate the tokens by colons, and a second time to remove commas after the name.  You may assume that the format of these lines always contains the same number of colon-separated tokens in the same order.

Shell scripts 

Put a comment header atop each of your script files indicating your name, course, etc. and a description of the program.  Each of your two scripts should also have a proper #! heading so that it can be used as an executable file. For reference, our mantra.sh is 26 lines (14 "substantive" lines in the CSE 142 Indenter tool) and our filestats.sh is 21 lines (13 "substantive").  You don't need to match these totals exactly or be close to them; they are just a ballpark.  

Answer:

.bash_profile文件如下:

mesg n

PS1="\u@\h:\w$"

fullName=$(grep "$USER" /etc/passwd | cut -f 5 -d ":" | cut -f 1 -d "," )
echo "Hello,$fullName!"
echo "Your shell is $SHELL."
echo "The current time is $(date)."

Task 2: Shell script, mantra.sh 

For this exercise, write a shell script file mantra.sh that accepts two command-line arguments: a string for a message to print, and a number of times to print it.  The script should print the message that many times, surrounded by a box of stars.  For example, if the user runs your script in the following way:

 ./mantra.sh "All work and no play makes Jack a dull boy" 5 

Then the script should produce the following output to the shell: ********************************************** 

* All work and no play makes Jack a dull boy * 

* All work and no play makes Jack a dull boy * 

* All work and no play makes Jack a dull boy * 

* All work and no play makes Jack a dull boy * 

* All work and no play makes Jack a dull boy * 

********************************************** 

Notice that you can pass a multi-word message by putting it in quotes.  (Bash handles that for you.) You may assume that the user will pass 2 parameters and that their values will be reasonable; the message will be nonempty and shorter in length than the width of your terminal, and the number of times will be a valid integer greater than 0. Hint: Perhaps the trickiest part of this script is getting the lines of stars above and below the message to be exactly the right length to surround the messages.  You can use an appropriate command to find out the length of the message and surrounding stars, then print a star this many times above/below.  Use echo and ` ` backticks to combine commands. 

Use the diff command to check that your output EXACTLY matches the output listed on the homework web page!! 

Answer: 

mantra.sh如下:

#!/bin/bash
str=$1
num=$2
let l=${#str}+4
for i in $(seq $l);do
    echo -n "*"
done
echo
for i in $(seq $2);do
    echo "* "$1" *"
done
for i in $(seq $l);do
    echo -n "*"
done
echo

Task 3: Shell script, filestats.sh 

For this exercise, write a shell script file filestats.sh that accepts an arbitrary number of file names as command-line arguments and prints information about each of the files: 

• number of lines in the file 

• number of blank lines in the file, and percentage of blank lines out of the total lines 

• number of characters and words in the file, and approximate number of characters per word (computed as the truncated integer division of total characters divided by total words) 

The web site file hw5.tar.gz contains some test files.  If you decompress it to the current directory and then run: 

./filestats.sh Jack.java lyrics.dat 

Then your script should produce EXACTLY the following output to the shell: 

Jack.java:   

    lines: 13   

    blank: 3 (23%)   

    chars: 369 in 44 word(s) (8 char/word) 

lyrics.dat:   

    lines: 27   

    blank: 2 (7%)   

    chars: 873 in 180 word(s) (4 char/word) 

Note that the shell expands command-line arguments containing wildcards before your script runs.  For example, if your directory has files file1.txt , file2.txt and file3.txt and you run filestats.sh *.txt, the $@ array doesn't contain the string "*.txt".  Instead it will contain three elements: "file1.txt" , "file2.txt", and "file3.txt".  You may assume that each file listed as a command-line argument will be readable by your script.  You may also assume that no file will be empty (0 lines, 0 characters).  But a file might contain 0 blank lines or might be arbitrarily large/small.  If no arguments are passed to your script, it should produce no output. 

Hint: It can be tricky to figure out the number of blank lines in a file.  You can see which lines of a file are non-blank by using the grep command and looking for lines that match the pattern "." (a dot).  This doesn't literally look for a period character; a dot represents any character in general.  (We'll learn more about this when we cover "regular expressions.") Use the diff command to check that your output EXACTLY matches the output listed on the homework web page!! 

Answer: 

filestats.sh如下:

#!/bin/bash
for file in $@; do
    line=`wc -l $file | cut -f 1 -d " "`
    let blank="$line-`grep "." $file | wc -l | cut -f 1 -d " "`"
    cha=`wc -m $file | cut -f 1 -d " "`
    word=`wc -w $file | cut -f 1 -d " "`
    echo $file:
    echo "  lines: "$line
    let rate="$blank*100/$line"
    echo "  blank: "$blank" ("$rate"%)"
    let rate="$cha/$word"
    echo "  chars: "$cha" in "$word" word(s) ("$rate" char/word)"
done


源码链接: https://pan.quark.cn/s/fa13cd6c6c8d Chrome浏览器作为一款备受青睐的网页浏览器,凭借其出色的稳定性和运行速度获得了广泛认可。 然而出于安全考量,Chrome系统默认不兼容ActiveX插件,因为ActiveX技术主要应用于Internet Explorer,它赋予网页内容与用户本地系统交互的能力,但同时也可能引发潜在的安全隐患。 不过在某些特定工作场景下,比如在企业内部网络环境或需要与老旧应用程序整合时,可能仍需在Chrome中启用ActiveX控件。 为此我们必须掌握在Chrome浏览器下加载和运用ActiveX的方法。 首先需要明确ActiveX的本质。 ActiveX是由微软设计的一种技术框架,旨在开发可在网页环境中运行的控件,这些控件能够完成多种功能,包括视频播放、应用程序组件运行或与硬件设备通信等。 ActiveX控件多以OCX(OLE控件)格式发布。 在Chrome浏览器中启用ActiveX需要采取额外措施,因为该浏览器本身并不支持此项技术。 以下是几种常见的解决方案: 1. **应用Chrome的兼容性设置**:部分Chrome版本提供了" --enable-internal-activex"命令行参数,可通过此参数使浏览器具备加载ActiveX控件的能力。 用户可在启动Chrome时,于快捷方式的目标路径后附加该参数来激活此功能。 例如:"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --enable-internal-activex。 2. **安装第三方插件**:市面上存在一些第三方插件,例如"IE Tab"或"ActiveX Con...
标题SpringBoot与微信小程序结合的健康饮食平台研究AI更换标题第1章引言介绍健康饮食平台的研究背景、意义、国内外研究现状、论文方法及创新点。1.1研究背景与意义阐述健康饮食平台在当前社会的重要性及其市场需求。1.2国内外研究现状分析国内外健康饮食平台的发展现状及趋势。1.3研究方法及创新点概述本文采用的研究方法和技术创新点。第2章相关理论总结健康饮食、SpringBoot及微信小程序的相关理论。2.1健康饮食理论介绍健康饮食的基本原则和营养学知识。2.2SpringBoot框架阐述SpringBoot框架的特点、优势及在项目中的应用。2.3微信小程序技术介绍微信小程序的开发技术、特点及其用户群体。第3章健康饮食平台设计详细介绍健康饮食平台的设计方案,包括前端和后端设计。3.1平台架构设计给出平台的整体架构、模块划分及交互流程。3.2数据库设计介绍数据库的设计思路、表结构及数据关系。3.3前后端交互设计阐述前后端数据交互的方式、接口设计及安全性考虑。第4章微信小程序实现介绍微信小程序的具体实现过程,包括页面设计、功能实现等。4.1页面设计与布局给出微信小程序的页面设计思路、布局及交互效果。4.2功能实现与测试详细介绍微信小程序各项功能的实现过程及测试方法。4.3用户体验优化阐述如何提升微信小程序的用户体验,包括界面优化、性能优化等。第5章平台测试与优化对健康饮食平台进行测试,并根据测试结果进行优化。5.1测试环境与数据介绍测试环境、测试数据及测试方法。5.2测试结果分析从功能、性能、用户体验等方面对测试结果进行详细分析。5.3平台优化策略根据测试结果提出平台优化策略,包括代码优化、功能改进等。第6章结论与展望总结本文的研究成果,并展望未来的研究方向。6.1研究结论概括本文的主要研究结论和平台实现效果。6.2展望指出本文研究的不足之处以及未来研究的方向和改进点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值