ตัวแปรจากคำสั่งแบบมีเงื่อนไข

ฉันมีสคริปต์บางตัวที่ฉันเป็นเจ้าของการใช้ Bash shell นั้น มีคำสั่ง find ภายในคำสั่งแบบมีเงื่อนไข

บางสิ่งเช่นนี้:

if [ -z $(find / -type f -perm -002) ] ; then echo "no world writable found"

โดยที่อย่างอื่นฉันต้องการแสดงสิ่งที่พบแทน world write perms found

ที่ฉันสามารถทำได้:

echo $(find / -type f -perm -002) has world write permissions

หรือตั้งค่าตัวแปรเป็น $(find / -type f -perm -002)

แต่สงสัยว่าจะมีวิธีที่ดีกว่าในการทำเช่นนี้หรือไม่ มีวิธีอื่นในการดึงเนื้อหาของคำสั่ง find มาเป็นตัวแปรหรือไม่?


person d3051    schedule 17.10.2014    source แหล่งที่มา
comment
เกิดอะไรขึ้นกับการกำหนดผลลัพธ์ของการค้นหาให้กับตัวแปร?   -  person ceving    schedule 17.10.2014
comment
result=$(find / -type f -perm -002) หรือ local result=$(find / -type f -perm -002) หากมีอยู่ในฟังก์ชันจะเป็นการใช้สำนวน   -  person helpermethod    schedule 17.10.2014
comment
โปรดทราบว่าในต้นฉบับ คุณต้องอ้างอิงการทดแทนคำสั่ง เพราะหากขยายเป็นมากกว่าหนึ่งคำ [ จะบ่นว่ามีตัวถูกดำเนินการมากเกินไป   -  person chepner    schedule 17.10.2014
comment
รายการนี้ค่อนข้างยาว ฝ่ายบริหารได้เน้นย้ำถึงการเปลี่ยนแปลงเพียงเล็กน้อยตามความต้องการ ดังนั้นเพียงแค่สำรวจความเป็นไปได้ที่ฉันไม่รู้จัก regex บ้าๆ บางตัวซึ่งจะใช้สิ่งที่มีอยู่ในนั้นเพื่อนำเสนอเนื้อหาในคำสั่ง find โดยไม่ทำให้มันทำงานอีกครั้ง   -  person d3051    schedule 17.10.2014


คำตอบ (3)


คุณเพียงแค่นำเอาต์พุตและเก็บไว้ในตัวแปร หากไม่ว่างเปล่าคุณสามารถพิมพ์เนื้อหาได้ ด้วยวิธีนี้คุณจะต้องรันคำสั่งเพียงครั้งเดียวเท่านั้น

RESULT=$(find / -type f -perm -002)
if [ -z "$RESULT" ]
then
    echo "no world writable found"
else
    echo "$RESULT has world write permissions"
fi
person Chris Maes    schedule 17.10.2014
comment
หลังจากดูแล้วนี่ดูเหมือนจะเป็นวิธีที่หรูหราที่สุด ฉันจะผลักดันกลับที่การจัดการเกี่ยวกับเรื่องนี้ ขอบคุณ! - person d3051; 17.10.2014

คุณสามารถใช้ sed เพื่อแทรกพาดหัวได้หากต้องการ

REPORT=$(find /tmp -type f -perm -002 | sed '1s/^/Found world write permissions:\n/')
echo ${REPORT:-No world writable found.}

หมายเหตุ: ดูเหมือนว่าตัวอย่างของคุณใช้งานไม่ได้ เนื่องจาก find สามารถส่งคืนได้มากกว่าหนึ่งบรรทัด

และ awk สามารถทำทั้งสองอย่างพร้อมกันได้:

find /tmp -type f -perm -002 | 
awk -- '1{print "Found world write permissions:";print};END{if(NR==0)print "No world writable found."}'
person ceving    schedule 17.10.2014
comment
นี่ฉลาดนะ ฉันเรียนรู้บางอย่างที่นี่ ขอบคุณสำหรับการตอบกลับของคุณ! - person d3051; 17.10.2014

หากคุณไม่รังเกียจที่จะไม่มีข้อความ no world writable found คุณสามารถใช้คำสั่ง find คำสั่งเดียวได้ เพียงเท่านี้:

find / -type f -perm -002 -printf '%p has world write permissions\n'

หากคุณต้องการจัดเก็บไฟล์ที่ส่งคืนเพื่อใช้ในอนาคต ให้จัดเก็บไว้ในอาร์เรย์ (สมมติว่า Bash):

#!/bin/bash

files=()

while IFS= read -r -d '' f; do
    files+=( "$f" )
    # You may also print the message:
    printf '%s has world write permissions\n' "$f"
done < <(find / -type f -perm -002 -print0)

# At this point, you have all the found files
# You may print a message if no files were found:

if ((${#files[@]}==0)); then
    printf 'No world writable files found\n'
    exit 0
fi

# Here you can do some processing with the files found...
person gniourf_gniourf    schedule 17.10.2014
comment
การดูแลผู้ลงคะแนนเสียงที่บ้าคลั่งจะอธิบายการกระทำของเขา/เธอหรือไม่? - person gniourf_gniourf; 23.10.2014