Linux中國

Bash 下如何逐行讀取一個文件

圖01:Bash 腳本:讀取文件並逐行輸出文件

Bash 腳本:逐行讀取文本文件並創建為 pdf 文件

我的輸入文件如下(faq.txt):

4|http://www.cyberciti.biz/faq/mysql-user-creation/|Mysql User Creation: Setting Up a New MySQL User Account
4096|http://www.cyberciti.biz/faq/ksh-korn-shell/|What is UNIX / Linux Korn Shell?
4101|http://www.cyberciti.biz/faq/what-is-posix-shell/|What Is POSIX Shell?
17267|http://www.cyberciti.biz/faq/linux-check-battery-status/|Linux: Check Battery Status Command
17245|http://www.cyberciti.biz/faq/restarting-ntp-service-on-linux/|Linux Restart NTPD Service Command
17183|http://www.cyberciti.biz/faq/ubuntu-linux-determine-your-ip-address/|Ubuntu Linux: Determine Your IP Address
17172|http://www.cyberciti.biz/faq/determine-ip-address-of-linux-server/|HowTo: Determine an IP Address My Linux Server
16510|http://www.cyberciti.biz/faq/unix-linux-restart-php-service-command/|Linux / Unix: Restart PHP Service Command
8292|http://www.cyberciti.biz/faq/mounting-harddisks-in-freebsd-with-mount-command/|FreeBSD: Mount Hard Drive / Disk Command
8190|http://www.cyberciti.biz/faq/rebooting-solaris-unix-server/|Reboot a Solaris UNIX System

我的 bash 腳本:

#!/bin/bash
# Usage: Create pdf files from input (wrapper script)
# Author: Vivek Gite <Www.cyberciti.biz> under GPL v2.x+
#------------------------------------------------------
#Input file
_db="/tmp/wordpress/faq.txt"

#Output location
o="/var/www/prviate/pdf/faq"

_writer="~/bin/py/pdfwriter.py"

# If file exists 
if [[ -f "$_db" ]]
then
    # read it
    while IFS=&apos;|&apos; read -r pdfid pdfurl pdftitle
    do
        local pdf="$o/$pdfid.pdf"
        echo "Creating $pdf file ..."
    #Genrate pdf file
        $_writer --quiet --footer-spacing 2 
        --footer-left "nixCraft is GIT UL++++ W+++ C++++ M+ e+++ d-" 
        --footer-right "Page [page] of [toPage]" --footer-line 
        --footer-font-size 7 --print-media-type "$pdfurl" "$pdf"
    done <"$_db"
fi

技巧:從 bash 變數中讀取

讓我們看看如何在 Debian 或者 Ubuntu Linux 下列出所有安裝過的 php 包,請輸入:

# 我將輸出內容賦值到一個變數名為 $list中 #

list=$(dpkg --list php* | awk &apos;/ii/{print $2}&apos;)
printf &apos;%sn&apos; "$list"

示例輸出:

php-pear
php5-cli
php5-common
php5-fpm
php5-gd
php5-json
php5-memcache
php5-mysql
php5-readline
php5-suhosin-extension

你現在可以從 $list 中看到它們,並安裝這些包:

#!/bin/bash
# BASH can iterate over $list variable using a "here string" #
while IFS= read -r pkg
do
    printf &apos;Installing php package %s...n&apos; "$pkg"
    /usr/bin/apt-get -qq install $pkg
done <<< "$list"
printf &apos;*** Do not forget to run php5enmod and restart the server (httpd or php5-fpm) ***n&apos;

示例輸出:

Installing php package php-pear...
Installing php package php5-cli...
Installing php package php5-common...
Installing php package php5-fpm...
Installing php package php5-gd...
Installing php package php5-json...
Installing php package php5-memcache...
Installing php package php5-mysql...
Installing php package php5-readline...
Installing php package php5-suhosin-extension...

*** Do not forget to run php5enmod and restart the server (httpd or php5-fpm) ***

via: http://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/

作者: VIVEK GIT 譯者:strugglingyouth 校對:wxy

本文由 LCTT 原創翻譯,Linux中國 榮譽推出


本文轉載來自 Linux 中國: https://github.com/Linux-CN/archive

對這篇文章感覺如何?

太棒了
0
不錯
0
愛死了
0
不太好
0
感覺很糟
0
雨落清風。心向陽

    You may also like

    Leave a reply

    您的郵箱地址不會被公開。 必填項已用 * 標註

    這個站點使用 Akismet 來減少垃圾評論。了解你的評論數據如何被處理

    More in:Linux中國

    Linux中國

    捐贈 Let&apos;s Encrypt,共建安全的互聯網

    隨著 Mozilla、蘋果和谷歌對沃通和 StartCom 這兩家 CA 公司處罰落定,很多使用這兩家 CA 所簽發證書的網站紛紛尋求新的證書籤發商。有一個非盈利組織可以為大家提供了免費、可靠和安全的 SSL 證書服務,這就是 Let's Encrypt 項目。現在,它需要您的幫助
    Linux中國

    關於Linux防火牆iptables的面試問答

    Nishita Agarwal是Tecmint的用戶,她將分享關於她剛剛經歷的一家公司(印度的一家私人公司Pune)的面試經驗。在面試中她被問及許多不同的問題,但她是iptables方面的專家,因此她想分享這些關於iptables的問題和相應的答案給那些以後可能會進行相關面試的人。 所有的問題和相應的答案都基於Nishita Agarwal的記憶並經過了重寫。 嗨,朋友!我叫Nishita Agarwal。我已經取得了理學學士學位,我的專業集中在UNIX和它的變種(BSD,Linux)。它們一直深深的吸引著我。我在存儲方面有1年多的經驗。我正在尋求職業上的變化,並將供職於印度的P
    Linux中國

    Lets Encrypt 已被所有主流瀏覽器所信任

    旨在讓每個網站都能使用 HTTPS 加密的非贏利組織 Lets Encrypt 已經得了 IdenTrust的交叉簽名,這意味著其證書現在已經可以被所有主流的瀏覽器所信任。從這個裡程碑事件開始,訪問者訪問使用了Lets Encrypt 證書的網站不再需要特別配置就可以得到 HTTPS 安全保護了。 Lets Encrypt 的兩個中級證書 ...