Linux Bash shell中的这些用法你都了解了吗? (一)

本文深入探讨了LinuxBashshell的个性化输出技巧,包括彩色文本显示、格式化输出及环境变量管理。此外,还介绍了文件描述符的高级用法和自定义方法,以及如何在函数中正确处理参数。

Linux Bash shell中的这些用法你都了解了吗? (一)

如何做一些个性化输出:

  1. echo
    echo 是bash shell中的常见输出命令。但是echo可以实现带颜色的文本输出。用法如下:
echo -e "\e[1;31m This is red text \e[0m"
[1;31m This is red text [0m

这里-e 表示可以处理带有转义字符的输入 \e[1;31m 这个转义字符串把字体颜色设置成红色,\e[0m把颜色进行重置

31是字体颜色代码。对于字体颜色有:
重置=0, 黑色=30, 红色=31, 绿色=32, 黄色=33, 蓝色=34, 品红色=35, 蓝绿色=36, 白色=37.

如果需要改变背景颜色,可以用下面来实现
echo -e "\e[1;42m Green Background \e[0m"
[1;42m Green Background [0m


对于背景颜色有
重置=0, 黑色=40, 红色=41, 绿色=42, 黄色=43, 蓝色=44, 品红色=45, 蓝绿色=46, 白色=47.
  1. printf printf可以在格式上做出更精确的控制,用法如下:
    printf "%-5s %-10s %-4s\n" No Name Score
    printf "%-5s %-10s %-4.2f\n" 1 Tom 80.2345
    printf "%-5s %-10s %-4.2f\n" 2 Jack 79.3542
    printf "%-5s %-10s %-4.2f\n" 3 Charles 75.5789
No    Name       Score
1     Tom        80.23
2     Jack       79.35
3     Charles    75.58

%s,%c,%d,%f 是替换符(s:string, c: character, d:int, f:float)学过c/c++的同学应该很容易理解这些

环境变量部分

  1. 如何查看一个进程相关的环境变量

cat /proc/$PID/environ

获取一个程序的的进程id可以用过pgrep 比如pgrep gedit
2. UID 是一个重要的环境变量,可以用来检查当前脚本是以一个root 用户还是一个普通用户运行的。

    if [ $UID -ne 0 ]; then
        echo "Non root user. Please run as root"
    else
        echo "Root user"
    fi
Non root user. Please run as root
  1. SHELL 是一个标志当前我们正在使用那个shell的环境变量
echo $SHELL
/bin/bash
  1. PS1 可以用来修改 bash shell的提示字符串
    默认的bash shell 提示字符串是类似这样的"username@hostname:~"原来的PS1定义如下:‘PS1=′" 原来的PS1定义如下: `PS1='"PS1PS1={debian_chroot:+($debian_chroot)}\u@\h:\w$ '通过修改PS1我们可以做到这样的PS1="这个帅帅的人输入了: "`

  2. 定义一个函数来是实现增加一个参数到指定环境变量

    prepend(){
        [ -d "$2" ] && eval $1=\"$2\$\{$1:+':'\$$1\}\" && export $1;
    }
    
  3. 从PS1和prepend这个函数中我们看到了如下形式的shell参数扩展

    ${parameter:+expression}

    这个表达式的含义是如果变量parameter被设置且其值不是null,那么这个表达式被扩展成expression,即"${parameter:+expression}“被替换为"expression”

文件描述符

我们都知道在linux 中,文件描述符0,1,2被保留,并且有以下含义:

  • 0: 标准输入
  • 1:标准输出
  • 2:标准错误

在shell编程中我们也经常用类似
cmd 2>stderr.txt 1>stdout.out 或者 cmd 2>&1 output.txt 或者 cmd >& output.txt 这些用法。

高级用法1: 但是如果我们如果急需要把数据重定向到一个文件,同时又需要把这部分数据作为标准输入提供给下一个命令,又该如何做呢?

比如我们想把ls 执行的结果输出到一个文件中,又想把ls输出的结果利用管道传给cat -n 命令。我们知道可以利用 ls > output.txt输出到文件,也可以利用通过 ls | cat -n 把ls结果输入给cat。 但是 ls > output.txt | cat -n 这样的命令只能把ls的输出重定向到output.txt,却无法再作为cat命令的参数了。

答案是通过 tee 命令

ls | tee output.txt | cat -n

这时ls的输出会保存在output.txt中,又能通过命令cat命令输出到屏幕上。

高级用法2: 如何自定义文件描述符?

比如说我们想执行类似于cat file.txt 这种把文件作为输入的命令,文件名在带上路径的情况下会很长比如/etc/lib/software/…/a.conf,每次都写那么长的文件名很繁琐,我们可以定义一个文件描述符比如3来绑定文件,这样每次只需要写成cat >&3 了。

如何做呢?

答案是通过exec 命令。

通过exec 命令,我们可以创建只读文件描述符®,清空写文件描述符(w),追加写文件描述符(a+)。

  • 只读权限文件描述符
    exec 3<input.txt #创建文件描述符3
    cat <&3 # 使用文件描述符3
    
  • 清空写文件描述符
    exec 4>output.txt #创建清空写文件描述符4
    ls >&4 #使用文件描述符4
    
  • 追加写文件描述符
    exec 5>>log.txt #创建清空写文件描述符5
    apache2 start >&5 #使用文件描述符5
    

关于函数的部分

在函数中如何接收传入的参数?

  • $0:脚本名称
  • $1:第一个参数
  • $2:第二个参数
  • $n:第n个参数
  • “$@” 返回所有的参数,形式为 “$1” “$2” “$3” …
  • “$*” 返回所有的参数,形式为 “$1c$2c$3”,这里c是IFS变量的第一个字符
  • $? 返回命令执行的结果

"@"和"@" 和"@""*"的区别
“$*” 是把所有参数作为一个单独的字符串返回.

一个利用"$@"的例子,定义一个repeat函数,这个函数的作用是重复执行指定命令,直到命令执行成功

repeat()
{
    while true
    do
        $@ && return
    done
}

或者更快的实现 repeat () { while :; do $@ && return ; done }
其用法为:repeat cmd parameter of the cmd

repeat weget -c http://www.example.com/test.tar.gz
Visual studio 2012 下 sqlite3的工程文件,生成32或64位的sqlite3库 This page explains how to compile SQLite with Microsoft Visual Studio.NET (aka VS.NET). Download Download and unzip the file sqlite_source.zip (or sqlite-amalgamation-x_x_x_x.zip). Do not use the .tar.gz files because they have not been pre-processed for use with Windows. Create a starter DLL project File > New > Project. Under Project Types, select Visual C++ Projects and then Win32. Choose the project template "Win32 Project". Give the project a name and click OK. When the "Win32 Application Wizard" appears, choose Application Settings. set the Application Type to DLL and check the box that says "Empty project". Click Finish. You now have a blank DLL project. Add the SQLite files to the project Project > Add Existing Item. Add all the .c and .h files that you unzipped, except for: tclsqlite.c and shell.c. Note: You may add tclsqlite.c and shell.c, but then you have to define the preprocessor-symbol NO_TCL. Click Project -> Properties, navigate to the C/C++-folder and choose "Preprocessor". In the field that says "Preprocessor definitions" add NO_TCL to the existing string, separated by a semicolon. Under "Code Generation" for "Runtime Library" make sure to pick static linking. /MTd (release) or /MTd (debug) Make a .DEF file A .def file should be placed in the project directory. Get the def file by downloading the zipped sqlite DLL file under the "Precompiled Binaries For Windows" in the download page. Add the sqlite[3].def file to the project. Under Project > Properties navigate to the Linker folder and choose "Input". In the field that says "Module Definition File" type sqlite[3].def. NOTE: You have to do this twice, once for the Debug configuration and once for the Release configuration. Compile! The next 3 steps maybe be required by some. I was able to build the DLL and produce a .lib file only following the above 12 steps. for VS 2005. In order to build the lib file so that an application can link against the sqlite[3].dll you will need to add a step to the post-build event. Right click on Project, select Properties, expand Build Events and type "LIB /DEF:\sqlite[3].def" into the Command line field, both for debug and release configurations, where is the location to the file sqlite[3].def. To compile 3.3.7(this may apply to other versions too), I had to do this extra step: Add the project directory to the include path, here's how to do it in details: Under Project > Properties navigate to the C/C++ folder and choose "General", In the field "Additional Include Directories" type "."(a single dot, which is the current directory) Repeat for each configuration (debug/release/Win32/x64). To compile 3.6.14.1 (maybe others too), I also had to: Go to Project > Properties. Open the C/C++ then Preprocessor folder. Add "SQLITE_ENABLE_COLUMN_METADATA" to the list of preprocessor definitions. Repeat for each configuration (debug/release/Win32/x64). How to make the SQLITE.EXE command-line utility There are some slight changes if you wanted to build the sqlite.exe command-line utility, instead of the DLL. To do that, when you're creating the project and you get to the "Win32 Application Wizard", choose "Console Application" instead of "DLL". Then, when you are adding files to the project, also add shell.c. Finally, don't include the .DEF file. The sqllite def for version 2 is. EXPORTS sqlite_open sqlite_close sqlite_exec sqlite_last_insert_rowid sqlite_error_string sqlite_interrupt sqlite_complete sqlite_busy_handler sqlite_busy_timeout sqlite_get_table sqlite_free_table sqlite_mprintf sqlite_vmprintf sqlite_exec_printf sqlite_exec_vprintf sqlite_get_table_printf sqlite_get_table_vprintf sqlite_freemem sqlite_libversion sqlite_libencoding sqlite_changes sqlite_create_function sqlite_create_aggregate sqlite_function_type sqlite_user_data sqlite_aggregate_context sqlite_aggregate_count sqlite_set_result_string sqlite_set_result_int sqlite_set_result_double sqlite_set_result_error sqliteMalloc sqliteFree sqliteRealloc sqlite_set_authorizer sqlite_trace sqlite_compile sqlite_step sqlite_finalize sqlite_progress_handler sqlite_reset sqlite_last_statement_changes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值