Mac 电脑放在环境变量中的通用脚本

mac电脑下放在环境变量中,方便提高效率执行

注:相关路径需要根据实际情况进行更新

需要在 .bash_profile 文件中定义如下(路径需要做实际替换):

bash 复制代码
source $HOME/software/scripts/base_profile.sh
source $HOME/software/scripts/custom_profile.sh

custom_profile.sh 通用内容如下:

bash 复制代码
# 将时间戳格式化为可读形式,示例: ts2 1714471312123 结果:2024-04-30 18:01:52.123
ts2() {
    local timestamp=$1
    # 检查输入是否纯数字
    if ! [[ "$timestamp" =~ ^[0-9]+$ ]]; then
        echo "错误:时间戳必须是数字!" >&2
        return 1
    fi
    # 检查位数(10位=秒级,13位=毫秒级)
    local length=${#timestamp}
    if (( length == 10 )); then
        # 秒级时间戳
        date -r "$timestamp" "+%Y-%m-%d %H:%M:%S"
    elif (( length == 13 )); then
        # 毫秒级时间戳
        local milliseconds=$((timestamp % 1000))
        local seconds=$((timestamp / 1000))
        date -r "$seconds" "+%Y-%m-%d %H:%M:%S.${milliseconds}"
    else
        echo "错误:时间戳必须是 10 位(秒级)或 13 位(毫秒级)!" >&2
        return 1
    fi
}

# 使用示例: error demo
error(){
  # 红底白字
  echo -e "\033[41;37m $1 \033[0m"
}

# 使用示例: info demo
info(){
  # 绿字
  echo -e "\033[47;32m $1 \033[0m"
}

# 需要修改相关路径
# buildandroidcmd(){
#   echo "注意需要在 build 目录执行,且确保相关代码能正常执行"
#   echo "构建 Android 平台可执行程序"
#   cmake .. -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-21
#   cmake --build .
# }

# adb cmd
alias a='adb shell'
alias akill='adb kill-server'
alias astart='adb start-server'
alias apush='adb push '
alias apull='adb pull '
alias areboot='adb reboot'
alias atop='adb shell dumpsys activity top'
alias astop='adb shell top'
alias arecovery='adb reboot recovery'
alias abootloader='adb reboot bootloader'
alias alog='adb logcat'
alias alogcrash='adb logcat -b crash'
alias aloge='adb logcat -v time *:E'
alias lsime='adb shell ime list -s'
alias lspkg='adb shell pm list packages'
alias img='imgcat'

alias lf='exa -l --time-style=full-iso'
alias lt='exa -T -l'
alias ll='exa -l'
alias grepnr='grep -n -r'

# other cmd
alias rmdir='rm -r'

alias obash='o ~/.bash_profile'
alias sbash="source ~/.bash_profile"

# git 相关操作
alias giturl="git remote get-url origin"
alias gurl="git remote get-url origin"
alias gbr="git br -a |grep \"\""
alias gpull="git pull"
alias gclone="git clone "
alias gst="git status "
alias gco="git co "
alias gcheck="git checkout "

alias kill9='kill -9 '

alias ahome='adb shell input keyevent 3'
alias aback='adb shell input keyevent 4'

# 修改后生效
# alias ocus='o $HOME/software/scripts/custom_profile.sh'
# 预期需要修改 author 后的内容
alias glastweek="git log --pretty=format:\"%an: %s\" --since=1.weeks --author=yongchao.yyc|grep yongchao"


setproxy(){
  echo "first we clear the config of proxy before"
  adb shell settings put global http_proxy :0 
  
  echo "now we wait for it to finish for 2 second "
  sleep 2

  echo "second we set the proxy"
  info "ip:${1}  port:${2}"
  adb shell settings put global http_proxy $1:$2
}

gitcheck(){
  git checkout -b $* origin/$*
}

# 需要按实际情况修改后生效
# code(){
#   Open -a /Applications/Code.app $1
# }


apkg(){
  adb shell dumpsys window |grep mCurrentFocus
}


# 需要修改完路径后使用
# pstack(){
#    ndk-stack -sym $1 -dump /Users/muyi/Desktop/engine/crash/$2
# }

tailf(){
  tail -f $*
}


gclonetag(){
  git clone $1 --branch $2 --single-branch
}


ft(){
  grep -rnw . -e $*
}

# 将指定 so 替换指定包名中的指定动态库(设备需要root), 使用示例:pushsoto libxxx.so com.demo.app
pushsoto(){
    if adb shell "command -v su" >/dev/null 2>&1; then
        echo "Device is rooted"
    else
        error "Device is not rooted, cant run this commond"
        return
    fi
    
    so_file_path=$1
    so_dir=$(dirname "$so_file_path")
    echo "so_dir is:"
    echo "\t$so_dir"

    so_name=$(basename "$so_file_path")
    echo "so_name is:"
    echo "\t$so_name"

    (
      cd $so_dir

      result=$(adb shell pm path $2)
      if [ $? -eq 0 ]
      then
          full_path=${result#package:}
          dir_path=$(dirname "$full_path")
          echo "app path:"
          echo "\t$dir_path"
          
          echo "push so to sdcard"
          push_result=$(adb push $so_name /sdcard/)
          echo "\t$push_result"
          
          echo "move so to"
          echo "\t$dir_path/lib/arm64"
          adb shell "su -c 'mv /sdcard/$so_name $dir_path/lib/arm64'"

          echo "run chmod command"
          adb shell "su -c 'chmod 777 $dir_path/lib/arm64/$so_name'"
      else
          error "Failed to execute adb command, please make sure you device is ok"
      fi
    )
}

pushsoto32() {
    if adb shell "command -v su" >/dev/null 2>&1; then
        echo "Device is rooted"
    else
        error "Device is not rooted, cant run this commond"
        return
    fi
    
    so_file_path=$1
    so_dir=$(dirname "$so_file_path")
    echo "so_dir is:"
    echo "\t$so_dir"

    so_name=$(basename "$so_file_path")
    echo "so_name is:"
    echo "\t$so_name"

    (
      cd $so_dir

      result=$(adb shell pm path $2)
      if [ $? -eq 0 ]
      then
          full_path=${result#package:}
          dir_path=$(dirname "$full_path")
          echo "app path:"
          echo "\t$dir_path"
          
          echo "push so to sdcard"
          push_result=$(adb push $so_name /sdcard/)
          echo "\t$push_result"
          
          echo "move so to"
          echo "\t$dir_path/lib/arm"
          adb shell "su -c 'mv /sdcard/$so_name $dir_path/lib/arm'"

          echo "run chmod command"
          adb shell "su -c 'chmod 777 $dir_path/lib/arm/$so_name'"
      else
          error "Failed to execute adb command, please make sure you device is ok"
      fi
    )
}

# sign(){
#   jarsigner -verbose -keystore /Users/muyi/software/key_store/sign.keystore -signedjar sign_$1 $1 lonewolf -digestalg SHA1 -sigalg MD5withRSA
# }

# dex2jar(){
#   sh /Users/muyi/software/tools/android_killer/dex2jar/d2j-dex2jar.sh $*
# }

# de(){
#   java -jar /Users/muyi/software/tools/android_killer/bin/apktool/apktool/ShakaApktool.jar  d $*
# }

# en(){
#   java -jar /Users/muyi/software/tools/android_killer/bin/apktool/apktool/ShakaApktool.jar  b $*
# }

# decode(){
#   java -jar /Users/muyi/software/tools/android_killer/apktool_240.jar d $*
# }

# apktool(){
#  java -jar /Users/muyi/software/tools/android_killer/apktool_270.jar $* 
# }

# jd(){
#   java -jar /Users/muyi/software/tools/android_killer/jdgui141.jar
# }

setime(){
  adb shell ime set $*
}

apkpath(){
  adb shell pm path $*
}

port(){
  lsof -i:$*
}

lsport(){
  lsof -i:$*
}

tap(){
  adb shell input tap $*
}

swipe(){
  adb shell input swipe $*
}

send(){
  adb shell am broadcast -a  $* 
}

ascheme(){
  adb shell am start $*
}

clearapp(){
  adb shell pm clear $*
}

kapp(){
  adb shell am force-stop $*
}

input(){
  adb shell am broadcast -a ADB_INPUT_TEXT --es msg $*
}

f(){
  grep -R $* .
}

aconip(){
  adb tcpip 5555
  sleep 3
  adb connect $*:5555
  adb devices -l
}

cbuild(){
  cmake .
  make
}

acon(){
  adb tcpip 5555

  sleep 3

  line="------------------------------------------\r\n"
  echo "now we fetch the ip of android device"
  aip=`aip`
  echo "ip is ${aip}"

  echo ${line}
  echo "now we connect"
  echo "${aip}:5555"
  adb connect "${aip}:5555"

  echo ${line}

  adb devices -l
}

adis(){
  adb disconnect
}

awifi(){
  adb tcpip 5555
  ip=`aip`
  echo "device's ip is ${ip}"
  adb connect ${ip}
}


# 获取 ip 地址
ip(){
  ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"
}

# 获取 ip 地址并拷贝到粘贴板(针对 mac os)
ipcp(){
  ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"| pbcopy
}

aip(){
  adb shell ifconfig |grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"
}

# sh 的封装,在脚本执行结束后弹窗提示
ish(){
  sh $*
  say script finish
  osascript -e 'display notification "脚本执行完成" with title "ish"'
}

kclient(){
  lsof -i tcp:12345 |grep ython | awk '{print $2}' |xargs kill -9
}

# 获取对应文件的行数
count(){
    wc -l $*
}

fsize(){
  du -sh $*
}

# 安装 apk ,并在结束时给予声音和通知栏提示(针对 mac os)
apk(){
  result=$(echo $* | grep "Amap")
  if [[ "$result" != "" ]]
  then
        info "now we try to uninstall amap first"
        una
        info "then we intall"
  fi
  adb install $*
  # 以下两行针对 mac os
  # say "install finish"
  osascript -e 'display notification "安装完成" with title "apk"'
  # if [[ "$result" != "" ]]
  # then
  #       oa
  # else
  #       echo "不包含"
  # fi
}

# 向鸿蒙手机安装包
hap(){
  hdc install $*
  osascript -e 'display notification "安装完成" with title "hap"'
}

# 在控制台打印信息并在mac通知中显示对应信息
shownoti(){
  echo $*
  osascript -e 'display notification "'$*'" with title "脚本"'
}

# 卸载指定包名的app
unapp(){
  adb uninstall $* 
}

# 使用 SublimeText 打开内容
# 需要修改对应路径
# o(){
#   Open -a /Applications/SublimeText.app $1
# }

# 查看 apk 包信息
apkdump(){
  aapt dump badging  $1
}


# 查看已连接设备
list(){
  adb devices -l
}

# 对Android设备执行录屏,ctrl+c结束录屏
arecord() {
  result=`adb devices -l |grep product |wc -l | tr -cd "[0-9]" `

  if [[ ${result} == "1" ]]; then
    echo "连接了一台设备"
  else  
    error "已连接 ${result} 台设备,请确保有且仅有一台设备连接"
    return
  fi

  if [ $# -eq 0 ]
  then
    name="record"
  else
    name="record$1"
  fi  
  trap 'onCtrlC' INT
  function onCtrlC () {
      running=false
      trap -- '' INT
  }

  info "开始录屏"
  adb shell screenrecord /sdcard/demo${name}.mp4

  info "录屏结束,等待录屏正常停止..."  
  sleep 5

  info "现在拉取视频..."
  target_dir="/Users/muyi/Pictures/videos"
  adb pull /sdcard/demo${name}.mp4 ${target_dir}/${name}.mp4
  info "视频已保存到:$target_dir"
  echo "文件名为:${name}.mp4"
  echo "完整路径为:${target_dir}/${name}.mp4"
}

# 对当前设备截图保存到命令执行所在目录且将其打开
# 需要修改路径
asp() {
  target_dir="/Users/muyi/Pictures/screenshots"

  # current=`date "+%Y-%m-%d %H:%M:%S"`     #获取当前时间,例:2015-03-11 12:33:41       
  # timeStamp=`date -f "$current" +%s`      #将current转换为时间戳,精确到秒
  # currentTimeStamp=$((timeStamp*1000+`date "+%N"`/1000000)) #将current转换为时间戳,精确到毫秒

  if [ $# -eq 0 ]
  then
    # name=${currentTimeStamp}".png"
    name="screenshot.png"
  else
    name="$1.png"
    if [ $# -eq 2 ]
    then
        target_dir=$2
    fi
  fi
  
  adb shell screencap -p /sdcard/${name}
  adb pull /sdcard/${name} ${target_dir}/${name}
  adb shell rm /sdcard/${name}
  echo "save to $target_dir/${name}"
  # for mac os
  open $target_dir"/"$name
}