Skip to content

Latest commit

 

History

History
133 lines (127 loc) · 2.43 KB

Bash_scripts.md

File metadata and controls

133 lines (127 loc) · 2.43 KB

Bash-Scripting setting

https://www.codecademy.com/learn/bash-scripting/modules/bash-scripting/cheatsheet

To add this permission to a file with filename: script.sh

chmod +x script.sh

The beginning of your script file should start with #!/bin/bash on its own line.

#!/bin/bash

To ensure that scripts in ~/bin/ are available, you must add this directory to your PATH within your configuration file

PATH=~/bin:$PATH

On Linux style shells, this is ~/.bashrc

On OSX, this is ~/.bash_profile.

在 macOS Catalina 之後,預設的 shell 已經從 bash 改為了 zsh

更改path

nano ~/.zshrc

加入到末行

export PATH=~/bin:$PATH

重新載入

source ~/.zshrc

簡單指令

輸出文字

echo "Hello World!"

To access the value of a variable, we use the variable name prepended with a dollar sign ($)

greeting="Hello"
echo $greeting

Conditionals

if [ $index -lt 5 ]
then
  echo $index
else
  echo 5
fi

Equal: -eq

Not equal: -ne

Less than or equal: -le

Less than: -lt

Greater than or equal: -ge

Greater than: -gt

Is null: -z

Equal: ==

Not equal: !=

Loops

for word in $paragraph
do
  echo $word
done
while [ $index -lt 5 ]
do
  echo $index
  index=$((index + 1))
done
until [ $index -eq 5 ]
do
  echo $index
  index=$((index + 1))
done

Inputs

read number
echo "You guessed $number"

Aliases

alias saycolors='./saycolors.sh'
alias saycolors='./saycolors.sh "green"'

source 目錄下的所有文件。* 是一個通配符,表示匹配 source 目錄下的所有文件和子目錄。

for filename in source/*

ex

#!/bin/bash
echo "Welcome to our world"
## 1.1.1
firstline=$(head -n 1 source/changelog.md)
read -a splitfirstline <<< $firstline
version=${splitfirstline[1]}
echo "You are building version" $version

echo 'Do you want to continue? (enter "1" for yes, "0" for no)'
read versioncontinue
if [ $versioncontinue -eq 1 ]
  then
    echo "OK"
  else
    echo "Please come back when you are ready"
fi

for filename in source/*
do
  echo $filename
done

if [ "$filename" == "source/secretinfo.md" ]
then
  echo "Not copying" $filename
else
  echo "Copying" $filename
  cp $filename build/.
fi

cd build/
ls
# We'll add more code here later
cd ..
echo "Build version $version contains:"
ls