CP2.bash shell Standard Output

本文介绍了Bash Shell中控制命令输出的各种方法,包括使用echo、printf和prinft内置命令进行输出,如何保留空格,增加格式控制,以及如何将输出重定向到文件。同时,讲解了错误输出的处理,如将标准输出和错误输出分开或合并到同一文件,并展示了如何避免意外覆盖已存在的输出文件。此外,还提到了利用head和tail命令处理文件的开头和结尾,以及如何在管道中连接多个命令。
摘要由CSDN通过智能技术生成

1.Writing Output to the Terminal/Window

Problem: you want some simple output from your shell commands.

Solution: Use the echo built-in command.

[maxwell@DBAMAXWELL learning_bash_shell]$ echo Please wait.
Please wait.
[maxwell@DBAMAXWELL learning_bash_shell]$ 
[maxwell@DBAMAXWELL learning_bash_shell]$ help echo
echo: echo [-neE] [arg ...]
    Write arguments to the standard output.
    
    Display the ARGs, separated by a single space character and followed by a
    newline, on the standard output.
    
    Options:
      -n        do not append a newline
      -e        enable interpretation of the following backslash escapes
      -E        explicitly suppress interpretation of backslash escapes
    
    `echo' interprets the following backslash-escaped characters:
      \a        alert (bell)
      \b        backspace
      \c        suppress further output
      \e        escape character
      \E        escape character
      \f        form feed
      \n        new line
      \r        carriage return
      \t        horizontal tab
      \v        vertical tab
      \\        backslash
      \0nnn     the character whose ASCII code is NNN (octal).  NNN can be
                0 to 3 octal digits
      \xHH      the eight-bit character whose value is HH (hexadecimal).  HH
                can be one or two hex digits
    
    Exit Status:
    Returns success unless a write error occurs.
[maxwell@DBAMAXWELL learning_bash_shell]$ help printf
printf: printf [-v var] format [arguments]
    Formats and prints ARGUMENTS under control of the FORMAT.
    
    Options:
      -v var    assign the output to shell variable VAR rather than
                display it on the standard output
    
    FORMAT is a character string which contains three types of objects: plain
    characters, which are simply copied to standard output; character escape
    sequences, which are converted and copied to the standard output; and
    format specifications, each of which causes printing of the next successive
    argument.
    
    In addition to the standard format specifications described in printf(1),
    printf interprets:
    
      %b        expand backslash escape sequences in the corresponding argument
      %q        quote the argument in a way that can be reused as shell input
      %(fmt)T   output the date-time string resulting from using FMT as a format
                string for strftime(3)
    
    The format is re-used as necessary to consume all of the arguments.  If
    there are fewer arguments than the format requires,  extra format
    specifications behave as if a zero value or null string, as appropriate,
    had been supplied.
    
    Exit Status:
    Returns success unless an invalid option is given or a write or assignment
    error occurs.
[maxwell@DBAMAXWELL learning_bash_shell]$

2.Writing Output but Preserving Spacing Problem

Problem : you want the output to preserve your spacing

Solution: Enclose the string in quotes

[maxwell@DBAMAXWELL learning_bash_shell]$ echo "this    was    very    widely    spaced"
this    was    very    widely    spaced
[maxwell@DBAMAXWELL learning_bash_shell]$ echo 'this     was    very    widely    spaced'
this     was    very    widely    spaced
[maxwell@DBAMAXWELL learning_bash_shell]$

3.Writing Output with More Formatting Control Problem

Problem: You want more control over the formatting and placement of output.

Solution: Use the prinf built-in command.

ep:

[maxwell@DBAMAXWELL learning_bash_shell]$ printf '%s = %d\n' Lines $LINES
Lines = 33
[maxwell@DBAMAXWELL learning_bash_shell]$ printf '%-10.10s = %4.2f\n' 'GigaHerz' 1.92735
GigaHerz   = 1.93
[maxwell@DBAMAXWELL learning_bash_shell]$ 

 

4.Writing Output Without the Newline

Problem: You want to produce some output without the default newline that echo provides.

Solution: Using printf it''s easy——just leave off the ending \n in your format string.With echo ,use the -n option.

[maxwell@DBAMAXWELL learning_bash_shell]$ printf "%s %s" next prompt           
next prompt[maxwell@DBAMAXWELL learning_bash_shell]$ 
[maxwell@DBAMAXWELL learning_bash_shell]$ 
[maxwell@DBAMAXWELL learning_bash_shell]$ echo -n prompt            
prompt[maxwell@DBAMAXWELL learning_bash_shell]$ 

5.Saving Output from a Command

Problem:You want to keep the output from a command by putting it in a file.

Solution: Use the > symbol to tell the shell to redirect the output into a file. 

ep: 

6.Saving Output to Other Files

Problem:You want to save the output with a redirect to elsewhere in the  filesystem,not in the current directory.

Solution:Use more of a pathname when you redirect the output.

ep: 

7.Saving Output from the ls Command

Problem:You tried to save output from the ls command with a redirect,but when you look at the resulting file, the format is not what you expected.

Solution:Use -C option on ls when you redirect the output.

 ep: 

8.Sending Both Output and Error Messages to Different Files

Problem:You are expecting output from a program but you don't want it to get littered with error messages.You'd like to save your error messages,but it's harder to find them mixed among the expected output.

 Solution:Redirct output and error message to different files.

9.Sending Both Output and Error Messages to The Same File

Problem:Using redirection ,you can redirect output or error messages to separate files,but how do you capture all the output and error messages to a single file?

Solution:Use the shell syntax to redirect standard error messages to the same place as standard output.

&> or >& is a shortcut that simply send both STDOUT and STDERR to the same place——exactly what we want to do.

 

 10.Appending Rather Than Clobbering Output

Problem:Each time you redirect your output ,it creates that output file anew.What if you want to redirect output a second time(or third,or ...),and don't want to clobber the previous output?

Solution:The double greater-than sign(>>) is a bash redirect that mean append the output.

11.Using Just the Beginning or End of a File

Problem:You need to display or use just the beginning or end of a file.

Solution:Use the head or tail commands.By default,head will output the first 10 lines and tail will output the last 10 lines of the given file.If more than one file is given ,the appropriate lines from each of them are output.Use the -number switch to change the number of lines .tail also has the -f and -F and -F switches,which follow the end of the file as it is written to.

12.Skipping a Header in a File

Problem:You have a file with one or more header lines and you need to process just the data, and skip the header.

Solution:Use the tail command with a special argument.

 

An argument to tail,which is a number starting dash(-),will specify a line offset relative to the end of the file.tail +1 file gives you the entire file, the same as cat.+2 skips the first line.

13.Throwing Output Away

Problem:Sometimes you don't want to save the output into a file

Solution:Redirect the output to /dev/null as shown 

14.Saving or Grouping Output from Serveral Commands

Problem:You want to capture the output with a redirect,but you're typing several commands on one line.

       $ pwd; ls; cd ../elsewhere; pwd; ls > /tmp/all.out

Solution: you could use parentheses () to tell bash to run the commands in a subshell,then redirect the output of the entire subshell's execution.

15. Connecting Two Programs by Using Output As Input 

Problem:You want to take the output from one program and use it as the input of another program.

Solution: you could redirect the output from the first program into a temporary file,then use that file as input to the second program.

16. Saving a Copy of Output Even While Using it as Input

Problem:You want to debug a long sequence of piped I/O.

Solution: The solution to these problems is to use what plumbers call a T-joint pipes.

[maxwell@DBAMAXWELL cp2]$ find / -name '*.c' -print > /tmp/all.my.sources   

[maxwell@DBAMAXWELL cp2]$ find / -name '*.c' -print | tee /tmp/all.my.sources

17.Connecting Two Programs by Using Output As Arguments

Problem:what if one of the programs to which you would like to connect with a pipe doesn't work that way?

Solution: Use the command substitution feature of bash

The $() encloses a command that is run in a subshell.

18.Saving Output When Redirect Doesn't Seem to Work

 Problem:You tried using > but some (or all) of the output still appears on the screen.

Solution: Redirect the error output

19.Swapping STDERR and STDOUT 

Problem:You need to swap STDERR and STDOUT so you can send STDOUT to a logfile,but then send STDERR to the screen and to a file using the tee command.But pipes only work with STDOUT.

Solution: Swap STDERR and STDOUT before the pipe redirection using a third file descriptor

[maxwell@DBAMAXWELL cp2]$ ./myscript 3>&1 1>stdout.logfile 2>&3- | tee -a stderr.logfile

20.Keeping Files Safe from Accidental Overwriting

Problem:You don't want to delete the contents of a file by mistake.It can be too easy to mistype a filename and find that you've redirected output into a file that you meant to save.

The noclobber option tells bash not to overwrite any existing files when yoou redirect output. 

21.Clobbering a File on Purpose

 Problem:You like to have noclobber set,but every once in a while you do want to clobber a file when you redirect output.Can you override bash's good intentions,just once?

Solution: Use >| to redirect your output.Even if noclobber is set,bash ingore its setting and overwrites the file.

CP2K是一个强大的量子力学计算软件包,常用于分子模拟和材料科学研究。CP2K 2024.1的安装教程可能会因操作系统的不同而有所差异,这里提供一个通用的大致步骤: 1. **系统需求**: - 检查您的计算机是否满足CP2K的最低硬件要求,如64位处理器、足够的内存和硬盘空间。 - 通常推荐使用支持OpenMP或多线程技术的操作系统,如Linux(Ubuntu、CentOS)、macOS或Windows(通过WSL或Docker)。 2. **下载安装文件**: 访问CP2K官方网站(https://www.cp2k.org/download),找到对应版本的二进制安装包或者源码。如果你需要编译从源代码,你需要Git等基本开发工具。 3. **安装前准备**: - 安装必要的依赖,比如Python(作为CP2K的脚本语言)、MPI库(用于并行计算)、BLAS和LAPACK(数值库)等。 4. **二进制安装**(适用于预编译的系统): - 双击运行下载的安装包,按照向导提示进行下一步操作,包括接受许可协议、选择安装路径等。 5. **源码编译**(适用于Linux): - 在终端中解压源码,进入目录。 ``` tar xvf cp2k-2024.1.tar.gz cd cp2k-2024.1 ``` - 配置环境,设置编译选项(如并行处理级别、优化等级等): ``` ./configure ``` - 编译并安装: ``` make && make install ``` 6. **配置环境变量**: - 添加CP2K到系统PATH,使命令行可以识别和执行CP2K程序。 7. **验证安装**: 运行`cp2k`命令检查是否能够正常启动,并测试一个简单的例子。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值