スクリプト内で wgrep 変数が見つかりません。

スクリプト内で wgrep 変数が見つかりません。

私は現在シェルスクリプトを再び学んでいます。私はr / EarthPornをチェックし、ランダムに投稿を選択し、その投稿に移動して画像をダウンロードするスクリプトを作成しています。その後、背景として設定します。

何らかの理由で私はこれを得ます:

URL transformed to HTTPS due to an HSTS policy
--2018-09-09 19:56:10--  https://www.reddit.com/r/EarthPorn
Resolving www.reddit.com (www.reddit.com)... 151.101.125.140
Connecting to www.reddit.com (www.reddit.com)|151.101.125.140|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 485053 (474K) [text/html]
Saving to: ‘STDOUT’

-                         100%[==================================>] 473.68K  1.69MB/s    in 0.3s    

2018-09-09 19:56:12 (1.69 MB/s) - written to stdout [485053/485053]

./wallpaper.sh: 13: ./wallpaper.sh: LINK: not found
http://: Invalid host name.
www.reddit.com/r/EarthPorn/comments/9ef7bi/picture_i_took_hiking_mount_sulphur_banff/

これが私が今まで持っているものです:

#!/bin/sh
wget -O - www.reddit.com/r/EarthPorn > file
#1 Get all the post links from the subreddit r/EarthPorn
grep -Po '(?<=href="https://www.reddit.com/r/EarthPorn/comments/)[^"]*' file > links
#2 Fix the links
sed -i -e 's~^~www.reddit.com/r/EarthPorn/comments/~' links
#3 Count the # of posts there are
POST_NUMBER="$(wc -l < links)"
#4 Choose a random number to pick which wallpaper we're going to use
NUMBER=$(shuf -i 1-$POST_NUMBER -n 1)
LINK=$(sed -n ${NUMBER}p < links)
wget -O - "$(LINK)" > picture
echo $LINK
#5 Get the picture link and save it

したがって、最初のwgetは正しく機能し、リンクには正しいリンクが含まれています。しかし、2番目のwgetで$ LINKが見つからないと言う理由はわかりません。私がエコーすると良いリンクが返され、私にはうまくいきます。同じリンクを使用してスクリプトの外部でwgetを実行すると、正常に動作します。アドバイスを少し得ることができますか?

ベストアンサー1

このシーケンスを使用する$(...)と、角かっこ内の内容が実行され、呼び出し元に出力が返されます。

例えば、

mydata=$(grep foobar myfile)

$mydataコマンドの結果として設定されますgrep

$LINKあなたの場合は、変数を拡張したいと思います。

あなたが考えることができるのは、${LINK}これが変数名が範囲を解釈するように強制する方法です。

たとえば、変数が見つかりますが、echo $a_b変数が見つかり、結果に追加されます。a_becho ${a}_ba_b

おすすめ記事