Я постараюсь сформулировать вопрос как можно более общим образом.
У меня есть скрипт-скрипт .sh, который имеет основную функцию. В нем он выполняет еще одну функцию parse_csv . Эта функция parse_csv позволяет ей принимать параметры, анализировать их с помощью getopts, а затем эти проанализированные переменные проверяются на типизацию.
Я нашел это аккуратным способом работы над bash что-то вроде проверки типа, поэтому я сделал так, чтобы все вспомогательные функции этого скрипта работали именно так.
В любом случае проблема возникла именно в этой части. кода.
Код: Выделить всё
script.sh
while getopts "r:f:n:l:w:h" opt; do
case $opt in
r) # Return host
RETURN_CONNECTION_STRING=${OPTARG}
;;
f) # URL list file
url_list=${OPTARG}
;;
n) # Project name
PROJECT_NAME=${OPTARG}
;;
l) # Byte limit
BYTE_LIMIT=${OPTARG}
;;
w) # Server username@IP:port
server_args+="-S ${OPTARG} "
((no_hosts++))
;;
h) # Show help
show_help
exit 0
;;
*) # Invalid option
echo "Invalid option: -$OPTARG" >&2
show_help
exit 1
;;
esac
done
main() {
## If process has been altered changes_file will exist, merge it first
if [[ -e ${changes_file} ]]; then
echo "An unmerged ${changes_file} was found !!" >&2
echo "Probably from an unfinnished process" >&2
echo update_csv -o ${url_list} -c ${changes_file}
update_csv -o "${url_list}" -c "${changes_file}"
rm "${changes_file}"
echo rm "${changes_file}"
fi
}
main
update_csv(){
## Given an original_file .csv with two columns
## And a changes_file
## - Same first column fields
## - Different second columnd fields
## - Rows may be disordered
## Update the original file keeping the order of rows
## It will update by creating a buffer_file
## This is made in mind for large files modification
## Should the process get interrupted
## buffer_file will be there
## PARSING ARGUMENTS ---------------------------------
## ---------------------------------------------------
# Initialize variables
original_file=""
changes_file=""
echo ${1} ${2} ${3} ${4}
# Parse options using getopts
while getopts "o:c:" opt; do
case "${opt}" in
o) original_file="${OPTARG}" ;;
c) changes_file="${OPTARG}" ;;
*)
echo "Usage: update_csv -o original_file.csv -c changes_file.csv" >&2
return 1
;;
esac
done
# Check if both original_file and changes_file are provided
if [[ -z "${original_file}" || -z "${changes_file}" ]]; then
echo "Usage: update_csv -o original_file.csv -c changes_file.csv" >&2
return 1
fi
}
Код: Выделить всё
./script.sh -f /home/fakuve/downloads/vim-dan/shopify2/https___shopify.dev.csv
Код: Выделить всё
Usage: update_csv -o original_file.csv -c changes_file.csv
Код: Выделить всё
rm: missing operand
Дает мне правильное расширение
Код: Выделить всё
update_csv -o /home/fakuve/downloads/vim-dan/shopify2/https___shopify.dev.csv -c /home/fakuve/downloads/vim-dan/shopify2/https___shopify.dev_ch.csv
Подробнее здесь: https://stackoverflow.com/questions/790 ... g-to-do-so