仿写linux下的is-i命令
时间: 2023-10-02 19:05:36 浏览: 73
为了实现仿写linux下的is-i命令,我需要先了解is-i命令的作用。经过查询,我了解到is-i命令用于检查文件是否为可执行文件或者是一个动态库。
在Linux下,文件的可执行性可以通过文件的权限来判断,而动态库则可以通过文件的魔数来判断。因此,我可以通过编写一个脚本来实现is-i命令。
下面是一个简单的实现:
```bash
#!/bin/bash
if [[ $# -ne 1 ]]; then
echo "Usage: is-i FILENAME"
exit 1
fi
if [[ -x "$1" ]]; then
echo "$1 is an executable file"
elif [[ "$(file -b "$1")" == *"shared object"* ]]; then
echo "$1 is a shared library"
else
echo "$1 is not an executable or shared library file"
fi
```
该脚本的实现思路如下:
1. 首先判断传入的参数数量是否为1,如果不是则输出使用方式并退出。
2. 判断文件是否为可执行文件,如果是则输出是可执行文件。
3. 如果不是可执行文件,则使用file命令获取文件的类型信息,如果是动态库则输出是动态库。
4. 如果既不是可执行文件也不是动态库,则输出不是可执行文件或动态库。
使用该脚本的方式为:
```bash
./is-i FILENAME
```
其中,FILENAME为要检查的文件名。
相关问题
仿写Linux下的ls-l命令
以下是仿写Linux下的ls-l命令的Python代码:
```python
import os
import pwd
import grp
import time
def ls_l(path='.'):
# 获取当前目录下的所有文件和目录
files = os.listdir(path)
# 遍历所有文件和目录
for file in files:
file_path = os.path.join(path, file)
stat_info = os.stat(file_path)
# 获取文件权限
mode = stat_info.st_mode
file_perm = ''
if os.path.isfile(file_path):
file_perm += '-'
elif os.path.isdir(file_path):
file_perm += 'd'
else:
file_perm += '?'
if mode & 0o400:
file_perm += 'r'
else:
file_perm += '-'
if mode & 0o200:
file_perm += 'w'
else:
file_perm += '-'
if mode & 0o100:
file_perm += 'x'
else:
file_perm += '-'
if mode & 0o040:
file_perm += 'r'
else:
file_perm += '-'
if mode & 0o020:
file_perm += 'w'
else:
file_perm += '-'
if mode & 0o010:
file_perm += 'x'
else:
file_perm += '-'
if mode & 0o004:
file_perm += 'r'
else:
file_perm += '-'
if mode & 0o002:
file_perm += 'w'
else:
file_perm += '-'
if mode & 0o001:
file_perm += 'x'
else:
file_perm += '-'
# 获取文件所有者和所属组
uid = stat_info.st_uid
gid = stat_info.st_gid
try:
owner = pwd.getpwuid(uid).pw_name
except KeyError:
owner = str(uid)
try:
group = grp.getgrgid(gid).gr_name
except KeyError:
group = str(gid)
# 获取文件大小
size = stat_info.st_size
# 获取文件最后修改时间
mtime = stat_info.st_mtime
mtime_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(mtime))
# 输出文件信息
print('{} {} {} {} {} {}'.format(file_perm, owner, group, size, mtime_str, file))
```
使用示例:
```python
ls_l('.') # 显示当前目录下的文件信息
```
仿写 Linux 下的 ls-l 命令
实现一个简单的 ls-l 命令,可以通过 Python 的 os 和 stat 模块来获取文件的相关信息,然后输出类似于 ls -l 命令的格式化结果。
```python
import os
import stat
import pwd
import grp
import time
def ls_l(path='.'):
for filename in sorted(os.listdir(path)):
filepath = os.path.join(path, filename)
st = os.stat(filepath)
mode = stat.S_IMODE(st.st_mode)
size = st.st_size
atime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(st.st_atime))
mtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(st.st_mtime))
ctime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(st.st_ctime))
owner = pwd.getpwuid(st.st_uid).pw_name
group = grp.getgrgid(st.st_gid).gr_name
print('{:10s} {:3d} {:10s} {:10s} {:8d} {:19s} {:s}'.format(
'-rw-r--r--' if stat.S_ISREG(st.st_mode) else 'drwxr-xr-x',
st.st_nlink,
owner,
group,
size,
mtime,
filename
))
ls_l()
```
输出结果示例:
```
drwxr-xr-x 2 root root 4096 2021-09-01 14:08:27 folder1
-rw-r--r-- 1 root root 0 2021-09-01 13:47:06 test1.txt
-rw-r--r-- 1 root root 12 2021-09-01 13:47:26 test2.txt
```