
*.sh
最初の行の shebang が欠落しているスクリプトがあります。 で修正できますかsed
?
答え1
インプレース演算i
でシェバン( ) を挿入します。sed
sed -i '1 i #!/bin/bash' file.sh
拡張子付きの元のファイルをバックアップする場合.bak
:
sed -i.bak '1 i #!/bin/bash' file.sh
#!/bin/bash
実際に必要なシェバンに置き換えます。
例:
% cat foo.sh
echo foobar
% sed '1 i #!/bin/bash' foo.sh
#!/bin/bash
echo foobar
答え2
bash
and を使用するcat
(インプレースではない):
cat <(echo '#!/bin/sh') foo.sh
または、GNU awk >= 4.1 を使用してインプレースで実行します。
awk -i inplace 'BEGINFILE{print "#!/bin/sh"}{print}' foo.sh