#author("2018-09-02T16:13:28+09:00","","")
#author("2018-09-02T16:14:43+09:00","","")
#nofollow
#norelated
総数:&counter(total); 今日:&counter(today); 昨日:&counter(yesterday);

* もくじ [#m93826d3]

#contents
* ''function func_select_element_from_array()'' [#a6186348]
 #!/bin/bash
 
 # function func_select_element_from_array()
 #   parameter1: name of given input array
 #   return value: array index for an element user selected
 function func_select_element_from_array(){
 	
 	local name_of_array=$1
 	eval local ref=\"\${${name_of_array}[@]}\"
 	local -a array=( ${ref} )
 	local -i size_of_array=${#array[@]}
 	
 	#for element in "${array[@]}" ; do
 	#	echo "${element}"
 	#done
 	
 	local is_first_time="true"
 	local -i index=0
 	while :
 	do
 		echo "size of array: ${size_of_array}"
 		echo "elements of array:"
 		for (( index=0 ; index < ${size_of_array} ; ++index )) 
 		do
 			echo "  [${index}] ${array[${index}]}"
 		done
 		
 		if [ ${is_first_time} = "true" ] ; then
 			echo -n "please select one of elements. (key in a number): "
 			is_first_time="false"
 		else
 			echo -n "please select one of elements again. (key in a number): "
 		fi
 		
 		local text="empty"
 		read text
 		echo "your input: ${text}"
 		
 		local text_except_num=`echo -n ${text} | sed -e "s/[0-9]//g"`
 		if [ "x" = "x${text}" ] || [ "x" != "x${text_except_num}" ] ; then
 			echo "数値ではないです"
 		else
 			echo "数値です"
 			index=$((${text}))
 			
 			if [ ${index} -ge ${size_of_array} ] ; then
 				echo "無効な数値です"
 			else
 				break
 			fi
 		fi
 	done
 	
 	return ${index}
 	
 }
 
 
 declare -a items=("item00" "item01" "item02" "item03")
 declare -i index_user_selected=999
 
 func_select_element_from_array items
 index_selected=$?
 echo "user selected item: ${items[${index_selected}]}"
 
 exit 0

*コマンド全体をルート権限で実行する [#t34cfaa0]
参考url https://qiita.com/naoyukisugi/items/801b676e8affe9a720f9

**sudoで実行した際の部分的な権限エラー [#f1a7a2d7]
コマンドラインで、リダイレクト(>>)を使ってファイルに書き込むと便利だが
 $ sudo echo "additional text" >> /mnt/windows_shared_folder/file.txt
のようにsudoを使ってルート権限で書き込もうとしても、>> /mnt/windows_shared_folder/file.txt のリダイレクトを行うコマンドが
一般ユーザーでの実行になってしまうためエラーが出てしまう。

**対処 [#w554c720]
 $ sudo bash -c '全体のコマンド'
とすることで、コマンド全体をルート権限で実行することができる。
 $ sudo bash -c 'sudo echo "additional text" >> /mnt/windows_shared_folder/file.txt'


トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS