Questions

  1. What is the printed value of the following code?
#!/bin/bash 
myfunc() { 
arr=$1 
echo "The array: ${arr[*]}" 
} 
 
my_arr=(1 2 3) 
myfunc ${my_arr[*]} 
  1. What is the output of the following code?
#!/bin/bash 
myvar=50 
myfunc() { 
myvar=100 
} 
echo $myvar 
myfunc 
  1. What is the problem with the following code? And how can you fix it?
clean_file { 
    is_file "$1" 
    BEFORE=$(wc -l "$1") 
    echo "The file $1 starts with $BEFORE" 
    sed -i.bak '/^\s*#/d;/^$/d' "$1" 
    AFTER=$(wc -l "$1") 
    echo "The file $1 is now $AFTER" 
} 
  1. What is the problem with the following code? And how can you fix it?
#!/bin/bash myfunc() { arr=$@ echo "The array from inside the function: ${arr[*]}" } test_arr=(1 2 3) echo "The origianl array is: ${test_arr[*]}" myfunc (${test_arr[*]}) ...

Get Mastering Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.