Shell script implements Ftok function _linux Shell

Source: Internet
Author: User

"The old Sys IPC still has several useful uses." Three types of IPC objects are shared memory, semaphores, and messages. "When you use these objects to IPC, you need to create a Key for each object." While it is theoretically possible to use any of our favorite keys when defining an IPC key, the key_t ftok (const char *path, int id) function is generally used in unix/linux to avoid duplication of definitions with other programs. into a relatively unique Key value. However, "Everyone hates System V IPC." It's slower than punched tape, using a completely unrelated namespace to the file system, naming its objects with human-hating numbers, and often forgetting their own objects, your system administrator often needs to use IPCS (1) to find the missing objects and delete them with IPCRM (1). And God forbid. Do not find problems after using up memory. "For the process monitoring/automatic Restart Bash Shell script, it is certainly not a good idea to hard-code the Key value in the script into the current program, preferably the Ftok function that implements the same algorithm."

Copy Code code as follows:

#!/bin/sh
Let key=0
function Ftok () {
pathname=$1;
proj_id=$2;

str_st_ino= ' stat--format= '%i ' ${pathname} ' 2>/dev/null ';
str_st_dev= ' stat--format= '%d ' ${pathname} ' 2>/dev/null ';
if ["X${str_st_ino}" = "x"-O "x${str_st_dev}" = "X"]; Then
return 1;
Fi

Let St_ino=${str_st_ino}
Let St_dev=${str_st_dev}

# Note that the bitwise operator here requires an escape character
Let KEY1=${ST_INO}\&16#FFFF
Let KEY2=${ST_DEV}\&16#FF
Let key2=${key2}\<\<16
Let KEY3=${PROJ_ID}\&16#FF
Let key3=${key3}\<\<24
Let Key=${key1}\|${key2}
Let Key=${key}\|${key3}
}

function Echohelp () {
echo "Ftok Generator"
echo "Usage:ftok pathname ProjId"
Exit 5
}

If [$#-ne 2]; Then
Echohelp
Fi

Spathname=$1
Let nprojectid=$2

if ["${spathname:0:1}"!= "/"]; Then
Spathname=${pwd}/${spathname}
Fi

if! Test-f ${spathname}; Then
echo "No File found! [${spathname}] "
Exit 4
Fi

Ftok "${spathname}" "${nprojectid}"
Echo ${key}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.