ปิดใช้งานแคชของไฟล์เรียกทำงานของ bash ในพา ธ


12

โปรดทราบว่านี่ไม่ซ้ำกัน ฉันกำลังถามเกี่ยวกับการปิดการใช้งานแคชไม่ล้างมัน หากคุณมีแคชเพื่อล้างข้อมูลจะไม่ถูกปิดใช้งาน

ในโอกาสที่หายากที่ฉันสังเกตเห็นแคชของ bash ของสิ่งที่พบในเส้นทางไม่ใช่เพราะมีประโยชน์เพราะเป็นเลือดที่น่ารำคาญ ตัวอย่าง:

~ dc$ export PATH=$HOME/bin:$PATH
~ dc$ cat bin/which
#!/bin/bash
echo "my which"
~ dc$ which
my which
~ dc$ rm bin/which
~ dc$ which which
-bash: /Users/dc/bin/which: No such file or directory

ในอีกเปลือก ...

~ dc$ which which
/usr/bin/which

ฉันแน่ใจว่าแคชนี้ใช้งานได้ดีในสมัยก่อนเมื่อดิสก์ช้าและหน่วยความจำมีราคาแพงและ จำกัด ดังนั้นคุณจึงไม่สามารถแคชได้มากนัก - การแคชพา ธ นั้นถูกกว่าการแคชบล็อกดิสก์ทั้งหมดที่จำเป็นในการค้นหาคำสั่ง . แต่ทุกวันนี้มันไม่ได้ให้ประโยชน์ที่เห็นได้ชัดเจนและทำให้เกิดปัญหามากกว่าที่จะแก้ มันเป็นความผิดพลาดซึ่งยืนยันว่าเป็นข้อผิดพลาด

และฉันไม่สามารถหาวิธีปิดใช้งานได้ ตัวชี้ใด ๆ


1
ประโยชน์ที่เห็นได้ชัดเจนแม้ในกรณีทั่วไปของเครื่องเดสก์ท็อปหากคุณไม่มี RAM มาก/usr/binเหลืออยู่ในแคช
Gilles 'หยุดความชั่วร้าย' ใน

1
@drhyde ขอโทษ ฉันทำเครื่องหมายคำถามนี้ว่าซ้ำกัน ใช้set +hเพื่อปิดการแฮ็ก
Evgeny Vereshchagin

ใน Nixos จะปิดใช้งานแฮชในการทุบตี ฉันคิดว่าด้วยเหตุผลที่ดีเพราะวิธีการทำงานของ Nixos อย่างไรก็ตามฉันไม่แน่ใจว่าสิ่งนี้จำเป็นสำหรับ Nixos หรือไม่ ฉันแค่พูดว่าการแฮชใน bash สามารถทำให้เกิดปัญหาได้ในบางสถานการณ์
typelogic

คำตอบ:


12

คุณสามารถลบไฟล์ที่ถูกแฮชได้ก่อนที่จะมีการดึงพรอมต์:

PROMPT_COMMAND='hash -r'

จากhelp hash:

hash: hash [-lr] [-p pathname] [-dt] [name ...]
Remember or display program locations.

Determine and remember the full pathname of each command NAME.  If
no arguments are given, information about remembered commands is displayed.

Options:
  -d                forget the remembered location of each NAME
  -l                display in a format that may be reused as input
  -p pathname       use PATHNAME is the full pathname of NAME
  -r                forget all remembered locations
  -t                print the remembered location of each NAME, preceding
            each location with the corresponding NAME if multiple
            NAMEs are given
Arguments:
  NAME              Each NAME is searched for in $PATH and added to the list
            of remembered commands.

Exit Status:
Returns success unless NAME is not found or an invalid option is given.

1
ดูคำตอบของฉันเกี่ยวกับset +h
Evgeny Vereshchagin

1
@EvgenyVereshchagin set +hไม่เหมาะอย่างยิ่งสำหรับการเรียกใช้ยูทิลิตี้ (เช่นการติดตั้งทับทิมอัญมณี) โทรออกhashสตรีมของ-bash: hash: hashing disabledคำเตือน
David Moles

ฉันยังเห็นข้อความเตือนเดียวกันในการเปิดใช้งาน python virtualenv แต่ฉันคิดว่ามันไม่เป็นอันตราย
typelogic

8

คุณสามารถบังคับให้ bash ทำการค้นหาเส้นทางใหม่ในกรณีที่ไม่มีคำสั่งในตารางแฮชอีกต่อไป

shopt -s checkhash

จาก manpage ของ bash:

checkhash

    หากตั้งค่าbashจะตรวจสอบว่ามีคำสั่งอยู่ในตารางแฮชก่อนที่จะพยายามดำเนินการ หากคำสั่ง hashed ไม่มีอยู่การค้นหาพา ธ ปกติจะดำเนินการ

ตัวอย่าง:

[blabla]$ PATH=$HOME/bin:$PATH
[blabla]$ hash -r
[blabla]$ cat bin/which
#!/bin/bash
echo "my which"
[blabla]$
[blabla]$ shopt -s checkhash
[blabla]$ which
my which
[blabla]$ mv bin/which bin/dis.which
[blabla]$ which which
/usr/bin/which
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.