More BASH questions
Matthew S-H
mathbymath at aol.com
Tue Jun 28 05:57:20 UTC 2005
I just have a few questions... I've looked in the "Advanced Bash-
Scripting Guide" as someone else had suggested. It has answered a
lot of my questions. However, I still have a few that I can't seem
to find the answer to. Anyone that could help me/answer a few of
these questions would have my gratitude.
Also, since this obviously isn't the right place to be asking these
questions, can anyone either give me a link to a more appropriate
listserv or (if they are BASH mavericks) offer to help me offlist??
This way I don't have to bother everyone.
1. How can I get find -exec to operate properly on "{" inside of a
replacement "thingy"? Example:
find -d ./* -exec echo "{}" ${{}//"e"/"h"} \;
2. I believe the following is interpreting "09" to be a ASCII
reference (not sure though). What would be a good workaround for this?:
user at host:~$ file_prompt.sh ~/test/
1:example_file1 7:example_file15 13:example_file6
2:example_file10 8:example_file16 14:example_file7
3:example_file11 9:example_file2 15:example_file8
4:example_file12 10:example_file3 16:example_file9
5:example_file13 11:example_file4
6:example_file14 12:example_file5
Choose: 09/Users/matt/my_bin/auxiliary_scripts/file_prompt.sh: line
64: 09: value too great for base (error token is "09")
/Users/matt/my_bin/auxiliary_scripts/file_prompt.sh: line 1: 09:
value too great for base (error token is "09")
-
3. Can someone please just look at my code and answer the following
2 questions?
a. How come the following is happening?
user at host:~$ Script_template.sh test "foo bar"
center.sh: String length greater than width!
b. Is there a better way to take in input from commands than "$(< /
dev/stdin)"?
========================================================================
========
file_prompt.sh
------------------------------------------------------------------------
--------
#!/bin/bash
E_FILE_DOES_NOT_EXIST=85
E_PERM_DENIED=87
E_FILE_NOT_DIR=88
E_NEED_ARGS=95
E_INVALID_USER_INPUT=98
work_dir="$1" # directory to work in
prompt="${2-"Choose"}" # prompt to show user
#chosen_num # Num from prompt
#chosen_file # Name of chosen file
max_tries=3 # Max num of prompts if invalid file
selected
if test -z $work_dir -o -z "$prompt"
then
echo "$(basename $0): Missing arguments!" > /dev/stderr
exit $E_NEED_ARGS
elif [[ ! -e "$work_dir" ]]
then
echo "$(basename $0): \"$work_dir\" does not exist!" > /dev/stderr
exit $E_FILE_DOES_NOT_EXIST
elif [[ ! -d "$work_dir" ]]
then
echo "$(basename $0): \"$work_dir\" is not a directory!" > /dev/
stderr
exit $E_FILE_NOT_DIR
elif [[ ! -x "$work_dir" ]]
then
echo "$(basename $0): You do not have permission to list the
files in \"$work_dir\"!" > /dev/stderr
exit $E_PERM_DENIED
fi
command ls "$work_dir" | grep -n -v 'randtonotmatch' | column > /dev/
stderr
num_files=$(command ls $work_dir | grep -c -v 'randtonotmatch')
digits=1
while (( $num_files >= 10 ))
do
((digits += 1))
((num_files /= 10))
done
num_files=$(command ls "$work_dir" | grep -c -v 'randtonotmatch')
tries_left=$((max_tries - 1))
read -p "$prompt: " -n $digits chosen_num > /dev/stderr
while [[ ( ! $num_files -ge $((chosen_num)) || $((chosen_num)) -eq
0 ) && $tries_left -ge 1 ]]
do
echo -e "\nInvalid file selection! You have $tries_left tries
left." > /dev/stderr
read -p "$prompt: " -n $digits chosen_num > /dev/stderr
((tries_left -= 1))
done
chosen_file=$(command ls "$work_dir" | grep -n -v 'randtonotmatch' |
grep ^$((chosen_num)): | cut -d ":" -f 2-)
if [[ $tries_left -eq 0 && ! -e "$chosen_file" ]]
then
echo
echo -e "$(basename $0): You have repeatedly chosen an invalid
file! This program will now quit." > /dev/stderr
exit $E_INVALID_USER_INPUT
fi
echo " - $chosen_file" > /dev/stderr
echo "$chosen_file" >&1
========================================================================
========
script_template.sh (3)
------------------------------------------------------------------------
--------
#!/bin/bash
filename="$1" # filename for script
description="$2" # script description
bw=50 # width of header box
#echo "$(center.sh $(split_string.sh "$description" 5) $bw '#' '#')"
>> $filename
echo "$(split_string.sh "$description" 5 | center.sh $(< /dev/stdin)
$bw '#' '#')" >> $filename
========================================================================
========
center.sh (3)
------------------------------------------------------------------------
--------
#!/bin/bash
E_WRONG_INPUT=97
text="$1"
width=$2
prefix="$3"
suffix="$4"
whitespace=$((width-${#text}))
if ((whitespace < 0))
then
echo "$(basename $0): String length greater than width!" > /dev/
stderr
exit $E_WRONG_INPUT
fi
odd_offset=$((whitespace % 2))
before="$(string_mult.sh ' ' $((whitespace / 2 - ${#prefix})) )"
after="$(string_mult.sh ' ' $((whitespace / 2 + odd_offset - $
{#suffix})) )"
echo "$prefix$before$text$after$suffix"
========================================================================
========
string_mult.sh (3)
------------------------------------------------------------------------
--------
#!/bin/bash
text="$1"
mult=$2
ans=""
while [ $mult -gt 0 ]
do
ans="$ans$text"
mult=$((mult - 1))
done
echo "$ans"
========================================================================
========
split_string.sh (3)
------------------------------------------------------------------------
--------
#!/bin/bash
string="$1"
max_per_section=$2
for word in "$string"
do
if (($((${#word} + ${#this_line})) < $max_per_section))
then
this_line="$this_line $word"
else
echo -n "$this_line" >&1
this_line="$word"
fi
done
echo "$this_line"
========================================================================
========
Thanks to all who respond,
~Matt
More information about the ubuntu-users
mailing list