This is for linux. The output is supposed to read user input, compile a list, append to the list as needed, and be in a list format of one item per line.
This is what I have so far. I am not getting the echo back on my list as my output and it is not appending either.
echo "Please input your list separating the items with a
space:"
read items
for items in $list;
do
echo $items
done >> shopping_list
echo "Your shopping list is:"
cat shopping_list
echo "Thank you for your grocery list!"
The below program will help in executing the required
operations:
#!/bin/bash -
echo "Enter items separated by a space:"
IFS= read -r LIST
IFS=' ' # split on space only
set -o noglob # disable glob
for item in $LIST; do
printf '%s\n' "$item" || break
done >> shopping_list
echo "Your shopping list is:"
cat shopping_list
echo "Thank you for your grocery list!"
Brief
Explanation:
1. IFS - IFS stands for Input Internal Field Separator -
it's a character that separates fields. Here we have taken space as
the character to separate the fields.
2. set -o noglob - the shell not to expand wildcard
characters in file names.
3. LIST- will store the value of list of items
separated with space.
4. After the end of for loop we
are displaying the file shopping_list using
cat.
5. printf - will do the echo back
of the list item.
Get Answers For Free
Most questions answered within 1 hours.