Bash 为访问的每个网页动态创建目录和文件

Bash 为访问的每个网页动态创建目录和文件,bash,applescript,Bash,Applescript,我写了以下脚本来记录我访问的网页。运行时,活动选项卡的文本内容将被记录并存储在目录中。我想能够做的是有一个新的目录和文件创建的飞行每次页面的变化;是否在同一选项卡中激活另一个选项卡或加载另一个url。正如现在一样,脚本将记录所有内容,但它将记录脚本启动时打开的网页目录中的所有内容。因此,如果在该页面处于活动状态时启动脚本,我访问的任何其他页面都将直接记录在该页面中记录的内容下方的同一文件和目录中 #!/bin/bash getChrome() { osascript -e \

我写了以下脚本来记录我访问的网页。运行时,活动选项卡的文本内容将被记录并存储在目录中。我想能够做的是有一个新的目录和文件创建的飞行每次页面的变化;是否在同一选项卡中激活另一个选项卡或加载另一个url。正如现在一样,脚本将记录所有内容,但它将记录脚本启动时打开的网页目录中的所有内容。因此,如果在该页面处于活动状态时启动脚本,我访问的任何其他页面都将直接记录在该页面中记录的内容下方的同一文件和目录中

#!/bin/bash

getChrome() {   
    osascript -e \
    'tell application "Google_Chrome" to tell the active tab of window 1 to execute javascript "document.body.innerText"'
}

url="$(osascript -e 'tell application "Google_Chrome" to set the_URL to the URL of the active tab of window 1')"
site="$(echo $url | cut -d/ -f3)" 

# removes . and replaces with _
if [[ $(echo "$site") =~ '.' ]]; then 
    site="$(echo "${site//./_}")"
fi

ds="$(date "+%m-%d-%y")"
dir="$HOME/Desktop/netlogs/$site/$ds"

if [ ! -d "${dir}" ]; then mkdir -p "${dir}"; fi

doc="${site}_LOG.txt"
file="${dir}/${doc}"

printf "\nBEGIN\n\n" | tee -a $file
while true
do 
    getChrome | while read lines
    do 
        echo $lines 
        # This if statement doesn't work.
        # Included here to show intent
        if [[ $url != $url ]]; then
            break 1
        fi  
    done 
done | awk '!seen[$0]++ { print; fflush() }' | tee -a $file
为了找出一种方法,我写了这个Applescript,以获得一些关于我更改选项卡时发生的事情的音频反馈:

tell application "Google_Chrome" to tell window 1
    tell the active tab
        set the_url to the URL
        repeat
            if the URL is not the_url then
                set the_url to the URL
                say "nope"
            else
                say "yep"
            end if
        end repeat
    end tell
end tell
除了非常烦人之外,它确实让我知道url的更改是可以识别的。但是我不知道当url改变时,如何利用同样的想法来创建一个新的目录和文件。我不是在寻找AppleScript解决方案。事实上,我更喜欢尽量避免使用AppleScript,因为它通常会带来头痛和最终的愤怒。但当它工作的时候,它工作的很好,我会很高兴有任何方法可以做到这一点

职能:
加上有趣的代码和“愤怒戒烟”(LOL)uno。我感觉到你的痛苦。祝你好运,谢谢!嘿,顺便问一下,你愿意和我分享一些智慧吗,好先生?指点、批评、想法、改进?嗯。。。也许我想得太多了。如果[[$url!=$url]],重新阅读代码是您获取测试
的主要兴趣所在;然后打破1;fi
要工作吗?当然你的意思是
如果[[“$prev_url”!=“$url”];然后打破1;你不是吗?;-)?我很抱歉没有早点回复。你上次写的评论让我用另一种方式思考。我真的想把事情弄清楚,这样我就知道了,而不是别人告诉我怎么做。。我希望这句话有意义。但我知道如何在每次url改变时创建新的目录。我想我会把它贴出来作为这个问题的答案。我仍然非常感谢您对其中任何部分的反馈。如果你喜欢的话,也可以随意发布一个更好或替代的答案<代码>[“$prev_url”!=“$url”]
是为我做的。非常感谢你。好的,很高兴你帮了忙。将你的改进版本作为你自己的答案发布,并接受它。我会对它发表评论,但在这里留下评论,这样我就知道你已经发布了。祝你好运。我觉得有一些小问题。在
tee-a
命令之后,我看不到需要
2>&1
。2.Posix脚本应该只包含Posix定义变量(HOME、PWD(和其他)的所有大写变量,Camel CASE被认为是shell脚本的正确用法。3、您可能有充分的理由使用%m-%d-%y作为日期格式,但使用
%y-%m-%d
会使文件自动排序,以便在您使用
ls-ltr
时在屏幕底部显示最新的文件。4.如果
[[$curr\u url='']
被认为是一个问题,那么您可能希望
退出1
.5。保持一致,dbl引用所有变量用法,尤其是
“$file”
。总的来说,这是一个非常好的代码,因此在将来,您应该使用“我能做得更好吗”类型的Qs征求意见。你最初的问题“为什么这不起作用”是关于主题;-)。。祝你好运,继续发帖。还在看。阅读时,我看不到什么
get$Browser |。。。。完成;<谢谢你的评论。你所说的
%Y-%m-%d
格式是我不知道的。那将非常有帮助。大写变量的原因很简单,就是写的可读性,因为当我的大脑变得一团糟时,它们更容易找到。
get$Browser | while read。。。。完成;<有点可笑,现在解决的问题产生了一个需要解决的新问题。现在,每当更改选项卡时,它都会根据需要创建一个新的目录和文档,并将信息写入该文件。但当您切换回上一个选项卡时,已写入该选项卡文件的信息将再次附加到同一文件中。如果它只写文档中尚未包含的新信息,那就更好了。但我会想一想,试着弄明白。如果我不能,我会问一个新问题。再次感谢你的帮助!!:-J
#!/bin/bash

getSAFARI() {   
    osascript -e \
    'tell application "Safari" to tell current tab of window 1 to do javascript "document.body.innerText"' 2>&1
}
getCHROME() {   
    osascript -e \
    'tell application "Google_Chrome" to tell active tab of window 1 to execute javascript "document.body.innerText"' 2>&1 
}
SURF() {
    if [[ "$(echo $current_url)" != "$(echo $url)" ]]; then
        echo -e "\nEND\n\n" | tee -a $file 2>&1
        break 1
    elif [[ "$(echo $current_url)" = https://example.com/*/ ]]; then
        regex='[[:alnum:]]'
    else
        regex='[\p{L}]'
    fi
    get$BROWSER | while read lines; do echo $lines | grep -E $regex; done < <(get$BROWSER)
}
SET_VARIABLES() {

    ds="$(date "+%m_%d_%y")"
    site="$(echo $url | cut -d/ -f3)"
    dir="$HOME/Desktop/netlogs/$ds/$site"
    doc="${site}_LOG.txt"
    file="${dir}/${doc}"
    if [ ! -d "${dir}" ]; then mkdir -p "${dir}"; fi
    echo '-----------------------------' | tee -a $file 2>&1
    date | tee -a $file 2>&1
    echo -ne "$url\n" | tee -a $file 2>&1
    echo '-----------------------------' | tee -a $file 2>&1
    printf "BEGIN\n\n" | tee -a $file 2>&1

}
echo "Choose your browser"
echo "1) Safari"
echo "2) Chrome"
echo
read -sn1 choice
while true
do
    case $choice in 
        [1])
        BROWSER="SAFARI"
        while true
        do
            url="$(osascript -e 'tell app "Safari" to tell the current tab of window 1 to get the url as text' 2>&1 | grep -v 'execution error')"
            if [[ $url = '' ]]; then echo "Now exiting" && exit; fi
            SET_VARIABLES
            while true
            do 
                current_url="$(osascript -e 'tell app "Safari" to tell the current tab of window 1 to get the url as text' 2>&1 | grep -v 'execution error')"
                SURF
                if [[ $current_url = '' ]]; then echo "Now exiting" && exit; fi
            done | awk '!seen[$0]++ { print; fflush() }' | tee -a $file
        done
        break
        ;;

        [2])
        BROWSER="CHROME"
        while true
        do
            url="$(osascript -e 'tell app "Google_Chrome" to tell the active tab of window 1 to get the url as text' 2>&1 | grep -v 'error')"
            if [[ $url = '' ]]; then echo "Now exiting" && exit; fi
            SET_VARIABLES
            while true
            do 
                current_url="$(osascript -e 'tell app "Google_Chrome" to tell the active tab of window 1 to get the url as text' 2>&1 | grep -v 'error')"
                SURF
                if [[ $current_url = '' ]]; then echo "Now exiting" && exit; fi
            done | awk '!seen[$0]++ { print; fflush() }' | tee -a $file
        done
        break
        ;;

        *)
        echo 'Invalid Selection'
        exit
        ;;
    esac
done