Common functions for BASH SCRIPT

A Demo to process command line arguments.

#!/bin/bash

# -----------------------------------------------------------------------------
# config item list
# -----------------------------------------------------------------------------
arg1="default value for arg1"
arg2="default value for arg2"
arg3="default value for arg3"

# -----------------------------------------------------------------------------
# functions
# -----------------------------------------------------------------------------
help()
{
  echo "Demo:"
  echo "$ $0 --arg1 'Demo' --arg2 '1.0.0' --arg3 '01'"
  return 0
}

# -----------------------------------------------------------------------------
# config item list
# -----------------------------------------------------------------------------
while [[ $# -ne 0 ]];
do
  case "$1" in
  -h|--help)
    help
    exit 0
  ;;
  --arg1)
    arg1=$2
    shift; shift;
  ;;
  --arg2)
    arg2=$2
    shift; shift
  ;;
  --arg3)
    arg3=$2
    shift; shift
  ;;
  *)
    echo "argument $1 not identified, use -h for help."
    exit 1
  ;;
  esac
done

# -----------------------------------------------------------------------------
# show/use values
# -----------------------------------------------------------------------------
echo "arg1=$arg1"
echo "arg2=$arg2"
echo "arg3=$arg3"

A demo to process log:

#!/bin/bash

LOG_CMD_FLAG=Yes
LOG_FILE=/var/tmp/logcmd.log

logcmd()
{
  case "$LOG_CMD_FLAG" in
  y*|Y*)
    date +"[%Y-%m-%d %H:%M:%S]" | tr -d '\n' >>$LOG_FILE
    echo " Run command [$@]" >>$LOG_FILE
    "$@" >>$LOG_FILE
  ;;
  *)
    return 0
  ;;
  esac
}

logcmd ls --color $HOME
logcmd df -h
logcmd tree .
logcmd ls $HOME
echo "Log file: $LOG_FILE"
echo "============================================================================="
cat $LOG_FILE
echo "============================================================================="


### CS162 Shell Homework Resources and Guidance For students enrolled in courses like CS198B[^1], understanding the foundational aspects of computer science is crucial, yet specific to CS162 which focuses on operating systems including extensive use of shell scripting, resources are tailored differently. #### Understanding Shell Scripting Basics Shell scripts provide a powerful way to automate tasks within an operating system environment. For those working through assignments related to CS162, it's important to familiarize oneself with basic commands such as `ls`, `cd`, `cp`, etc., along with more advanced features like loops (`for`, `while`), conditionals (`if`, `case`), functions, and error handling mechanisms. ```bash #!/bin/bash # Example script demonstrating simple loop structure. for file in $(ls); do echo "Processing $file" done ``` #### Utilizing Online Documentation Leveraging official documentation from sources like GNU (GNU Operating System)[^4] can be invaluable when tackling complex problems associated with Unix-like environments. Manuals pages accessible via command line using `man <command>` offer detailed explanations about each tool available under Linux distributions. #### Participating in Community Forums Engagement with communities dedicated to programming languages and tools used during this course—such as Stack Overflow or Reddit’s r/unixporn—can facilitate learning by allowing peers to share insights into common challenges faced while completing similar coursework. #### Exploring Educational Platforms Platforms offering tutorials specifically designed around Bash scripting could serve as supplementary material outside formal education settings provided at institutions teaching CS106S[^2]. Websites like Codecademy or freeCodeCamp often feature interactive lessons that guide learners step-by-step towards mastering these skills independently. --related questions-- 1. What are some key differences between bash and other shells? 2. How does one effectively debug shell scripts? 3. Can you recommend any books focused solely on UNIX/Linux shell scripting? 4. Are there particular editors better suited for writing efficient shell code?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值