if[-n] 、tr的用法

if [ -n ] 正确使用方法

if [ str1 = str2 ]       当两个串有相同内容、长度时为真 
if [ str1 != str2 ]      当串str1和str2不等时为真 
if [ -n str1 ]       当串的长度大于0时为真(串非空) 
if [ -z str1 ]        当串的长度为0时为真(空串) 
if [ str1 ]         当串str1为非空时为真

shell 中利用 -n 来判定字符串非空。
 

tr命令


1. tr 命令的使用


1.It can be used to perform substitution of characters,deletion of the characters, and squeezing of repeated characters from the standard input.
2.tr accepts input only through stdin (standard input) and cannot accept input through command-line arguments.【tr命令仅仅支持标准输入,不支持参数输入】
3.tr [options] set1 set2【如果set1 和 set2的长度不等,那么就会出现映射不匹配问题。】
4.ROT13 is a well-known encryption algorithm. The ROT13 scheme performs alphabetic rotation of characters for 13 characters.【ROT13 是一个著名的加密算法。字母往后13的字母就是加密后的字符】


如下这个就是使用ROT13作为加密算法使用,同时使用tr命令测试

[root@server4 backup]# echo "tr came, tr saw, tr conquered." | tr 'a-zA-Z' 'n-za-mN-ZA-M'
ge pnzr, ge fnj, ge pbadhrerq.
1
2


将文件file.txt中的内容作为tr命令的输入,然后将其中的空格转换成+

[root@server4 backup]# tr ' ' '+' < file.txt
hello+spark+\n+hello+hadoop
hello+hive
1
2
3


-d 参数


tr has an option -d to delete a set of characters that appear on stdin by using the specified set of characters to be deleted as follows:

$ cat file.txt | tr -d '[set1]'
#Only set1 is used, not set2【只需要使用set1,不需要参数set2】
1
2


将字符串中的指定内容删除

[root@server4 backup]# echo "hello 123 world 456" | tr -d '0-9'
hello  world 
1
2


-c 参数


Here,the complement set is the set containing all numerals,space characters,and newline characters.All other characters are removed since -d is used with tr.
-c参数指定的集合会被保留
 

[root@server4 backup]# echo hello 1 char 2 next 4 | tr -d -c '0-9 \n'
 1  2  4


Here,the complement set is the set containing all numerals,space characters,and newline characters.All other characters are removed since -d is used with tr.
1
2
3


-s 参数
压缩重复的字符。
tr provides the -s option to squeeze repeating characters from the input.
$[ operation ] performs a numeric operation

[root@server4 shells]# cat sum.txt | echo $[ $(tr '\n' '+') 0 ]
15

Difference between 'if -e' and 'if -f'

[ -e FILE ] True if FILE exists.

This will return true for both /etc/hosts and /dev/null and for directories.

[ -f FILE ] True if FILE exists and is a regular file.

This will return true for /etc/hosts and false for /dev/null (because it is not a regular file), and false for /dev since it is a directory.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<a href="contact?action=view&id=${contact.id}">View</a> <a href="contact?action=edit&id这段代码是一个主菜单的功能,用户可以选择登录、注册、管理员或退出。在用户选择完功能后,=${contact.id}">Edit</a> <a href="contact?action=delete&id=${contact.id}">Delete</a> </td> </tr> </c:forEach> </tbody> </table> <a href="contact?action=new程序会询问用户是否继续,如果选择继续则回到主菜单,如果选择退出则程序结束。 如果您">Add Contact</a> </body> </html> ``` contact-form.jsp ``` <%@ page language="java" contentType="想修改这段代码,需要先确定要修改的具体内容。以下是一些可能的修改建议: 1. 添加text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java菜单选项:您可以在菜单中添加新的选项,例如“借书”、“还书”等功能。 .sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Contact Form</title> </head> <body> <h1>Contact Form</h1> <form method="post" action2. 修改菜单文本:您可以修改菜单的文本内容,例如将“管理员”改为“管理中心="contact"> <input type="hidden" name="action" value="${contact.id==0 ? 'new' : 'edit'}"> ”。 3. 修改程序流程:您可以修改程序的流程,例如当用户选择登录时,可以要求用户输入用户名 <c:if test="${contact.id!=0}"> <input type="hidden" name="id" value="${contact.id}"> 和密码,而不是直接进入用户菜单。 4. 修改程序逻辑:您可以修改程序的逻辑, </c:if> <label>Name:</label> <input type="text" name="name" value="${contact.name}"> 例如添加一些错误处理代码,使程序更健壮。 具体的修改方法需要根据您的需求来定, <br> <label>Phone:</label> <input type="text" name="phone" value="${contact.phone}"> <您可以在代码中进行修改,或者重新编写一个新的函数来实现您的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值