菜鸟求教如何编写为最新的脚本赋予运行权限的脚本

sh/bash/dash/ksh/zsh等Shell脚本
回复
Arthur19891106
帖子: 30
注册时间: 2008-04-30 19:57
来自: http://godorz.cn
联系:

菜鸟求教如何编写为最新的脚本赋予运行权限的脚本

#1

帖子 Arthur19891106 » 2009-09-17 0:33

标题有点拗口..请问下怎么样得到最新脚本的文件名..ls -t?然后怎样拿第一个字符串?怎样抛掉空格给开的其他字符串..谢谢.
头像
Jarson
帖子: 2371
注册时间: 2008-07-21 9:44
来自: 深圳
联系:

Re: 菜鸟求教如何编写为最新的脚本赋予运行权限的脚本

#2

帖子 Jarson » 2009-09-17 1:30

代码: 全选

ls -lt|sed -e'2!d'|awk '{print $8}'
t3swing
帖子: 1028
注册时间: 2008-11-01 21:42
来自: 树下板凳

Re: 菜鸟求教如何编写为最新的脚本赋予运行权限的脚本

#3

帖子 t3swing » 2009-09-17 9:43

代码: 全选

chmod u+x $(file $(ls -t ) |sed -ne '/shell script text executable/p'|sed -e 's/://'|awk '{print $1}' |sed -n '1p')
低效的达到目的。
楼上得到的是最新修改的文件,没判断是否为脚本,高手继续
民族的脊梁,是踏实做事的人,非只知道骂街的泼妇。
Arthur19891106
帖子: 30
注册时间: 2008-04-30 19:57
来自: http://godorz.cn
联系:

Re: 菜鸟求教如何编写为最新的脚本赋予运行权限的脚本

#4

帖子 Arthur19891106 » 2009-09-17 11:25

t3swing 写了:

代码: 全选

chmod u+x $(file $(ls -t ) |sed -ne '/shell script text executable/p'|sed -e 's/://'|awk '{print $1}' |sed -n '1p')
低效的达到目的。
楼上得到的是最新修改的文件,没判断是否为脚本,高手继续
用起来很好很强大,严重感谢~~
头像
darkfish
帖子: 90
注册时间: 2009-09-18 10:03
来自: 北京
联系:

Re: 菜鸟求教如何编写为最新的脚本赋予运行权限的脚本

#5

帖子 darkfish » 2009-09-18 11:06

t3swing 写了:

代码: 全选

chmod u+x $(file $(ls -t ) |sed -ne '/shell script text executable/p'|sed -e 's/://'|awk '{print $1}' |sed -n '1p')
低效的达到目的。
楼上得到的是最新修改的文件,没判断是否为脚本,高手继续
其实这里用 ls -t是有点问题的。
建议用 "ls -l | sort -k6,7 -r" 来代替 :em11
头像
darkfish
帖子: 90
注册时间: 2009-09-18 10:03
来自: 北京
联系:

Re: 菜鸟求教如何编写为最新的脚本赋予运行权限的脚本

#6

帖子 darkfish » 2009-09-18 12:05

代码: 全选

#!/bin/bash

files=`ls -l | sort -k6,7 -r| awk '{print $8}'`

for subfile in $files
do
    file "$subfile" | grep "executable" > /dev/null
    if [ $? -eq 0 ];then
        chmod u+x -v "$subfile"
        exit 0
    fi
done
这个应该可以了。t3swing 对各工具的掌握不错啊,学习了,学习了 :em01 :em01
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 菜鸟求教如何编写为最新的脚本赋予运行权限的脚本

#7

帖子 aerofox » 2009-09-18 12:32

我也来一个,可以处理 "a test script" 这样的文件名:

代码: 全选

#!/bin/bash
latest_script=
for f in *; do
    if file -b "$f" | grep -q script; then
        if [[ ( -z "latest_script" ) || ( "$f" -nt "$latest_script" ) ]]; then
            latest_script="$f"
        fi
    fi
done

if [ -n "$latest_script" ]; then
    echo chmod a+x "$latest_script"
fi
我在 chmod 前加了个 echo 是为了调试用的,觉得可以了就把它去掉。
头像
darkfish
帖子: 90
注册时间: 2009-09-18 10:03
来自: 北京
联系:

Re: 菜鸟求教如何编写为最新的脚本赋予运行权限的脚本

#8

帖子 darkfish » 2009-09-18 13:27

aerofox 写了:我也来一个,可以处理 "a test script" 这样的文件名:

代码: 全选

#!/bin/bash
latest_script=
for f in *; do
    if file -b "$f" | grep -q script; then
        if [[ ( -z "latest_script" ) || ( "$f" -nt "$latest_script" ) ]]; then
            latest_script="$f"
        fi
    fi
done

if [ -n "$latest_script" ]; then
    echo chmod a+x "$latest_script"
fi
我在 chmod 前加了个 echo 是为了调试用的,觉得可以了就把它去掉。
学习了,我发现了在这个上面为别人解答问题对自己提高很有帮助啊。 :em06 :em06
学习学习 :em11 :em11
回复