write a shell script that accepts name from user and
creates a directory by the path name, then creates a text
file in that directory and stores in it, the data accepted
from user till STOP, displays the no. of characters stored
in the file.Program stops if directory name is null

Answer Posted / geichel

#!/bin/bash

echo -n "Enter directory name:"
read x
[[ ${x:='xX'} == 'xX' ]] && echo "error: No filename" &&
exit 0

mkdir --parents "/tmp/$x" 2> /dev/null
echo -n '' > "/tmp/$x/$x.txt"

echo "Enter text. Type 'STOP' on a line by itself to terminate."
while read y
do
[[ "$y" == "STOP" ]] && break;
echo "$y" >> /tmp/"$x"/$x.txt
done

a=$(wc -c "/tmp/$x/$x.txt" | cut -f1 -d' ')
echo
echo "file: /tmp/$x/$x.txt has $a characters"
exit 0;

Is This Answer Correct ?    2 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do I read a .sh file?

533


How can the contents of a file inside jar be read without extracting in a shell script?

599


What are the zombie processes?

569


How to print the first array element?

559


How many fields are present in a crontab file and what does each field specify?

590






Is cmd a shell?

599


Hello all, This is my assignment on shell scripting, can anyone help me regarding this ? Create a shell script which connects to the database In second shell script include the first script for the DB connection Create a table (PRADEEP_DATA) with 2 columns (name, value) In Third shell script include the first script for the DB connection And insert/delete the values from the Table, by accepting input from the user This functionality should be a menu driven Program: 1) Insert to the database a. Name b. value 2)Delete from the database a.Name b.value Exception handling needs to be taken care.

1857


What are the different types of commonly used shells on a typical linux system?

556


How do I run a script from command prompt?

564


How can I set the default rwx permission to all users on every file which is created in the current shell?

912


c program to display the information of given file similar to givan by the unix or linux command ls -l

1701


What is the meaning of $1 in shell script?

600


What is a shell script in windows?

590


Can you write a script to portray how set –x works?

557


What is the use of .sh file?

613