Guest User

Untitled

a guest
Dec 10th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. # echo "Hello World"
  4. # echo $BASH
  5.  
  6. # name=Farid
  7. # echo "My name is $name"
  8.  
  9. # echo "Let's me know your name : "
  10. # read name1 name2 name3
  11. # echo "Hi, $name1, $name2, $name3"
  12.  
  13. # read -p "username: " user_var
  14. # # using ss4 flag to hide input
  15. # read -sp "password: " user_pass
  16. # echo
  17. # echo "your username is $user_var"
  18. # echo "your password is $user_pass"
  19.  
  20. # echo "Enter names : "
  21. # read -a names
  22. # echo "Names : ${names[0]} ${names[1]}"
  23.  
  24. # echo "Enter names : "
  25. # read
  26. # echo "Names : $REPLY"
  27.  
  28. #try running this file like this ./hello.sh Farid1 2 Test
  29. # echo "parameter1: $1, parameter2: $2, parameter3: $3"
  30. # args=("$@")
  31. # echo "${args[0]}, ${args[1]}, ${args[2]}"
  32. # echo $@
  33.  
  34. # If structure on Bash Script
  35. # if [ condition ]
  36. # then
  37. # statement
  38. # fi
  39.  
  40. # count=10
  41. # if [ $count -gt 9 ]
  42. # then
  43. # echo "condition is true"
  44. # fi
  45. #
  46. # if (( $count > 9 ))
  47. # then
  48. # echo "condition is true"
  49. # fi
  50. #
  51. # word="abc"
  52. # word=a
  53. # if [ $word == "abc" ]
  54. # then
  55. # echo "condition is true"
  56. # fi
  57. #
  58. # if [[ $word < "b" ]]
  59. # then
  60. # echo "b is greater than a true"
  61. # elif [[ $word == "a" ]]
  62. # then
  63. # echo "a is true"
  64. # else
  65. # echo "condition is false"
  66. # fi
  67.  
  68. # get file exists
  69. # echo -e "Enter the name of the file : \c"
  70. # read file_name
  71.  
  72. # if [ -e $file_name ]
  73. # then
  74. # echo "$file_name found"
  75. # else
  76. # echo "$file_name not found"
  77. # fi
  78. #
  79. # check if file exists and regular files
  80. # echo -e "Enter the name of the file : \c"
  81. # read file_name
  82. #
  83. # if [ -f $file_name ]
  84. # then
  85. # echo "$file_name found"
  86. # else
  87. # echo "$file_name not found"
  88. # fi
  89. #
  90. # check if folder exists and its a directory
  91. # echo -e "Enter the name of the folder : \c"
  92. # read folder_name
  93. #
  94. # if [ -d $folder_name ]
  95. # then
  96. # echo "$folder_name found"
  97. # else
  98. # echo "$folder_name not found"
  99. # fi
  100. #
  101. # check if file empty or not
  102. # echo -e "Enter the name of the file : \c"
  103. # read file_name
  104.  
  105. # if [ -s $file_name ]
  106. # then
  107. # echo "$file_name not empty"
  108. # else
  109. # echo "$file_name empty"
  110. # fi
  111.  
  112. # echo -e "Enter the name of the file : \c"
  113. # read file_name
  114.  
  115. # if [ -f $file_name ]
  116. # then
  117. # if [ -w $file_name ]
  118. # then
  119. # echo "Type some text data. to quit press ctrl+d."
  120. # cat >> $file_name
  121. # else
  122. # echo "The file do not have permissions"
  123. # fi
  124. # else
  125. # echo "$file_name not exists"
  126. # fi
  127.  
  128. # age=25
  129. # if [ "$age" -gt 18 ] && [ "$age" -lt 30 ]
  130. # then
  131. # echo "valid age"
  132. # else
  133. # echo "age not valid"
  134. # fi
  135. #
  136. # age=25
  137. # if [ "$age" -gt 18 -a "$age" -lt 30 ]
  138. # then
  139. # echo "valid age"
  140. # else
  141. # echo "age not valid"
  142. # fi
  143. #
  144. # age=25
  145. # if [[ "$age" -gt 18 && "$age" -lt 30 ]]
  146. # then
  147. # echo "valid age"
  148. # else
  149. # echo "age not valid"
  150. # fi
  151. #
  152. # age=25
  153. # if [[ "$age" -eq 18 || "$age" -eq 30 ]]
  154. # then
  155. # echo "valid age"
  156. # else
  157. # echo "age not valid"
  158. # fi
  159. #
  160. # age=25
  161. # if [ "$age" -eq 18 -o "$age" -eq 30 ]
  162. # then
  163. # echo "valid age"
  164. # else
  165. # echo "age not valid"
  166. # fi
  167.  
  168. # num1=20
  169. # num2=5
  170. #
  171. # echo $(( $num1 + $num2 ))
  172. # echo $(( $num1 - $num2 ))
  173. # echo $(( $num1 * $num2 ))
  174. # echo $(( $num1 / $num2 ))
  175. # echo $(( $num1 % $num2 ))
  176. #
  177. # num1=20
  178. # num2=5
  179. # echo $(expr $num1 + $num2 )
  180. # echo $(expr $num1 - $num2 )
  181. # echo $(expr $num1 * $num2 )
  182. # echo $(expr $num1 / $num2 )
  183. # echo $(expr $num1 % $num2 )
  184. #
  185. # num1=20
  186. # num2=5
  187. # echo "$num1+$num2" | bc
  188. # echo "$num1-$num2" | bc
  189. # echo "$num1*$num2" | bc
  190. # echo "scale=20;$num1/$num2" | bc
  191. # echo "$num1%$num2" | bc
  192. #
  193. # num3=4
  194. # echo "scale=2;sqrt($num3)" | bc -l
  195. # echo "scale=2;$num3^2" | bc -l
  196.  
  197. # vehicle=$1
  198. #
  199. # case $vehicle in
  200. # "car" )
  201. # echo "Rent of $vehicle is 100 dollars" ;;
  202. # "van" )
  203. # echo "Rent of $vehicle is 80 dollars" ;;
  204. # "bicycle" )
  205. # echo "Rent of $vehicle is 5 dollars" ;;
  206. # "truck" )
  207. # echo "Rent of $vehicle is 150 dollars" ;;
  208. # * )
  209. # echo "Unknown vehicle" ;;
  210. # esac
  211.  
  212. # echo -e "Enter some character : \c"
  213. # read value
  214. #
  215. # case $value in
  216. # [a-z] )
  217. # echo "User entered $value a to z" ;;
  218. # [A-Z] )
  219. # echo "User entered $value A to Z" ;;
  220. # [0-9] )
  221. # echo "User entered $value 0 to 9" ;;
  222. # ? )
  223. # echo "User entered $value special character" ;;
  224. # * )
  225. # echo "Unknown vehicle" ;;
  226. # esac
  227.  
  228. # os=("ubuntu" "windows" "kali")
  229. # os[6]=mac
  230.  
  231. # unset os[2]
  232. # echo "${os[@]}"
  233. # echo "${os[1]}"
  234. # get index
  235. # echo "${!os[@]}"
  236. # get length
  237. # echo "${#os[@]}"
  238.  
  239. # while loops
  240. # while [ condition ]
  241. # do
  242. # command1
  243. # command2
  244. # command3
  245. # done
  246. #
  247. # n=1
  248. #
  249. # while [ $n -le 10 ]
  250. # do
  251. # echo "$n"
  252. # n=$(( n + 1 ))
  253. # done
  254. #
  255. # while (( $n <= 10 ))
  256. # do
  257. # echo "$n"
  258. # n=$(( n + 1 ))
  259. # done
  260. #
  261. # while (( $n <= 10 ))
  262. # do
  263. # echo "$n"
  264. # (( n++ ))
  265. # sleep 1
  266. # done
  267.  
  268. # content of the file will be pass to $p
  269. # while read p
  270. # do
  271. # echo "$p"
  272. # done < test.txt
  273. #
  274. # cat hello.sh | while read p
  275. # do
  276. # echo "$p"
  277. # done
  278. #
  279. # -r flg is going to prevent backslashes escapes from being interpreted
  280. # while IFS= read -r line
  281. # do
  282. # echo "$line"
  283. # done < test.txt
  284.  
  285. # until loops structure
  286. # until [ condition ]
  287. # do
  288. # command1
  289. # command2
  290. # command3
  291. # ...
  292. # ...
  293. # commandN
  294. # done
  295. #
  296. #n=1
  297. #
  298. # until [ $n -ge 10 ]
  299. # do
  300. # echo "$n"
  301. # (( n++ ))
  302. # done
  303. #
  304. # until (( $n >= 10 ))
  305. # do
  306. # echo "$n"
  307. # (( n++ ))
  308. # done
  309.  
  310. # for loops
  311. # for VARIABLE in 1 2 3 4 .. N
  312. # do
  313. # command1
  314. # command2
  315. # ...
  316. # commandN
  317. # done
  318. #
  319. # OR
  320. #
  321. # for VARIABLE in file1 file2 file3
  322. # do
  323. # command1 on $VARIABLE
  324. # command2
  325. # ...
  326. # commandN
  327. # done
  328. #
  329. # OR
  330. #
  331. # for OUTPUT in $(Linux-Or-Unix-Command-Here)
  332. # do
  333. # command1 on $OUTPUT
  334. # command2 on $OUTPUT
  335. # ...
  336. # commandN
  337. # done
  338. #
  339. # OR C expression
  340. #
  341. # for (( exp1; exp2; exp3 ))
  342. # do
  343. # command1
  344. # command2
  345. # ...
  346. # commandN
  347. # done
  348. #
  349. # read -p "Input multiple : "
  350. # n=()
  351. # for VARIABLE in $REPLY
  352. # do
  353. # n+=('-d')
  354. # n+=($VARIABLE)
  355. # done
  356. # echo ${n[@]}
  357. #
  358. # for i in {1..10}
  359. # do
  360. # echo $i
  361. # done
  362. #
  363. # for i in {1..10}
  364. # do
  365. # echo $i
  366. # done
  367. #
  368. # for (( i=0; i<5; i++ ))
  369. # do
  370. # echo $i
  371. # done
  372.  
  373. # for command in ls pwd date
  374. # do
  375. # echo "--------$command--------"
  376. # $command
  377. # done
  378. #
  379. # for item in *
  380. # do
  381. # if [ -d $item ]
  382. # then
  383. # echo $item
  384. # fi
  385. # done
  386.  
  387. # select loops
  388. # select varName in list
  389. # do
  390. # command1
  391. # command2
  392. # ...
  393. # commandN
  394. # done
  395. #
  396. # select name in Andi Budi Santi
  397. # do
  398. # case $name in
  399. # Andi)
  400. # echo "Andi selected"
  401. # ;;
  402. # Budi)
  403. # echo "Budi selected"
  404. # ;;
  405. # Santi )
  406. # echo "Santi selected"
  407. # ;;
  408. # *)
  409. # echo "Error please provide the no. between 1..3"
  410. # ;;
  411. # esac
  412. # done
  413.  
  414. # Break & Continue statement
  415. # for (( i=1; i<10; i++ ))
  416. # do
  417. # if [ $i -gt 5 ]
  418. # then
  419. # break
  420. # fi
  421. # echo "$i"
  422. # done
  423. #
  424. # for (( i=1; i<10; i++ ))
  425. # do
  426. # if [ $i -eq 5 -o $i -eq 6 ]
  427. # then
  428. # continue
  429. # fi
  430. # echo "$i"
  431. # done
  432.  
  433. # function
  434. # function name(){
  435. # commands
  436. # }
  437. #
  438. # name () {
  439. # commands
  440. # }
  441. #
  442. # function Hello(){
  443. # echo "Hello"
  444. # }
  445. #
  446. # quit () {
  447. # exit
  448. # }
  449. #
  450. # quit
  451. # Hello
  452. #
  453. # function print(){
  454. # echo $1 $2
  455. # }
  456. #
  457. # print Hello World
  458.  
  459. # function print(){
  460. # local name=${@}
  461. # echo $name
  462. # }
  463. #
  464. # print Hello World
  465. # echo $name
  466.  
  467. # example use of function
  468. # usage() {
  469. # echo "You need to provide an argument : "
  470. # echo "usage : $0 file_name"
  471. # quit
  472. # }
  473. #
  474. # is_file_exist() {
  475. # local file=$1
  476. # [[ -f "$file" ]] && return 0 || return 1
  477. # }
  478. #
  479. # quit() {
  480. # exit
  481. # }
  482. #
  483. # [[ $# -eq 0 ]] && usage
  484. # if ( is_file_exist "$1" )
  485. # then
  486. # echo "File found"
  487. # else
  488. # echo "File not found"
  489. # fi
  490.  
  491. # Read-only command
  492. # var=71
  493. # readonly var
  494. # var=50
  495. # echo "var => $var"
  496. # hello() {
  497. # echo "Hello world"
  498. # }
  499. # readonly -f hello
  500. # hello() {
  501. # echo "Hello world again"
  502. # }
  503. # hello
  504.  
  505. # Signals and Traps
  506. # trap "echo Exit command detected" 0
  507. # echo "Hello"
  508. # exit 0
  509. #
  510. # check signal from > man signal
  511. # trap "echo Exit command detected; exit" 0 2 15
  512. # echo "pid is $$"
  513. # while (( COUNT < 10 ))
  514. # do
  515. # sleep 10
  516. # (( COUNT ++ ))
  517. # echo $COUNT
  518. # done
  519. # exit 0
  520.  
  521. # Debug Script
  522. # use bash -x ./hello.sh
  523. # or you can specify -x options in the top (ex. /bin/bash -x)
  524. # or you can use like this
  525. # trap "echo Exit command detected; exit" 0 2 15
  526. # set -x
  527. # echo "pid is $$"
  528. # set +x
  529. # while (( COUNT < 10 ))
  530. # do
  531. # sleep 10
  532. # (( COUNT ++ ))
  533. # echo $COUNT
  534. # done
  535. # exit 0
Add Comment
Please, Sign In to add comment