How to read the user inputted file name?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read the user inputted file name?
# 1  
Old 03-26-2012
How to read the user inputted file name?

hello,
I have prepared a script

------------------------------------------------
Code:
#!/bin/sh
cat test.txt| sed '1d' | sed '$d' > /tmp/tmp1.txt
while read line
do
   typeset -i count=`echo $line | tr ' ' '}' | wc -c`
   finalcount=`expr $count - 1`
   echo $finalcount
done < /tmp/tmp1.txt

------------------------------------------------
now, test.txt is hardcoded.....i want to check how can my code consider the file which the user specifies.... in short i want to read the file which will be inputted by the user.
please helpSmilie

Last edited by Franklin52; 03-26-2012 at 03:27 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 03-26-2012
Quote:
Originally Posted by rac
hello,
I have prepared a script

------------------------------------------------
#!/bin/sh
cat test.txt| sed '1d' | sed '$d' > /tmp/tmp1.txt
while read line
do
typeset -i count=`echo $line | tr ' ' '}' | wc -c`
finalcount=`expr $count - 1`
echo $finalcount
done < /tmp/tmp1.txt
------------------------------------------------
now, test.txt is hardcoded.....i want to check how can my code consider the file which the user specifies.... in short i want to read the file which will be inputted by the user.
please helpSmilie
You can do it in two ways,
1. command line option
2. read command

First Method:
Consider this script name is 1.sh. you have to call this script,
1.sh test.txt
Code:
#!/bin/sh
cat $1| sed '1d' | sed '$d' > /tmp/tmp1.txt
while read line
do
typeset -i count=`echo $line | tr ' ' '}' | wc -c`
finalcount=`expr $count - 1`
echo $finalcount
done < /tmp/tmp1.txt

Second Method:
Consider this script name is 1.sh. you have to call this script,
1.sh

Code:
#!/bin/sh
echo "Enter the file name:"
read filename
cat $filename | sed '1d' | sed '$d' > /tmp/tmp1.txt
while read line
do
typeset -i count=`echo $line | tr ' ' '}' | wc -c`
finalcount=`expr $count - 1`
echo $finalcount
done < /tmp/tmp1.txt

In second method, the script waits until you give input manually,

Cheers,
RangaSmilie
# 3  
Old 03-26-2012
Thnx a ton !! it worked Smilie Smilie

---------- Post updated at 01:11 AM ---------- Previous update was at 01:04 AM ----------

Now, with the same file, if I want to store the values of variable 'finalcount' in a file or a log file, along with the line numbers...how do i achieve it?
thnx in advance
# 4  
Old 03-26-2012
thnx a lot dipak. ur words are true buti would appreciate if you give me the answer as well(if you know).
# 5  
Old 03-26-2012
The UNIX philosophy, precept #9, by Mike Gancarz

Always remember to "Make every program a filter":

script.sh:
Code:
#!/bin/bash

proc ()
{
        while read -r line
        do
                typeset -i count=`echo -E "$line" | tr ' ' '}' | wc -c`
                finalcount=`expr $count - 1`
                echo $finalcount
        done
}

FILE=
while read -r LINE
do
        FILE=`{ echo -E "$FILE" ; printf "$LINE\n" ; }`
done
FILE=`echo "$FILE" | sed '1d'`

TMP1TXT=`echo -E "$FILE" | sed '1d' | sed '$d'`

echo -E "$TMP1TXT" | proc

This way you can instead do:
Code:
$ cat test.txt | ./script.sh

A big warning, though: be careful what you feed this baby, because read -r and echo -E may still modify your input by parsing it!

BTW, I noticed you replaced all whitespace with curly braces but wc -c also counts the whitespaces, I mean, if that was your reason for using tr. And also, I think you may remove typeset -i without altering the functionality of your script, if all you want to do is count the amount of characters per line.

The thing would end up looking something like this:
Code:
#!/bin/bash

proc ()
{
        while read -r line
        do
                count=`echo -E "$line" | wc -c`
                finalcount=`expr $count - 1`
                echo $finalcount
        done
}

FILE=
while read -r LINE
do
        FILE=`{ echo -E "$FILE" ; printf "$LINE\n" ; }`
done
FILE=`echo "$FILE" | sed '1d'`

TMP1TXT=`echo -E "$FILE" | sed '1d' | sed '$d'`

echo -E "$TMP1TXT" | proc


Last edited by vomv1988; 03-26-2012 at 04:20 AM..
# 6  
Old 03-26-2012
Quote:
Originally Posted by dipak.warade
You need to read some good books on scripting first and then try

Thanks
Dipak WaradeSmilie
I would recommend Bruce Blinn's "Portable shell programming, an extensive collection of bourne shell examples", along with Seebach's "Portable Shell Scripting: From Novice to Professional" and, ofcourse, Mendel Cooper's famous (and free!) book "Advanced bash scripting guide". And, if you want just a quick and dirty introduction, you can always have a look at "Bourne Shell Programming" by mister Robert P. Sayle (another free, online book).
These 2 Users Gave Thanks to vomv1988 For This Post:
# 7  
Old 03-26-2012
thnx .....
but still...in general if i want to store the values of variables in a log file, how can i do it..?
My desired output in the log file

-----------------------------------
Line num finalcount
2 3
3 4
4 4
. .
. .
. .
. . and so on
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Other user should not read the file but can execute

Hi, I have one script for which I want that other user should not read the script file but can execute. Is there any method ? I tried by giving 711 but it gives Permission denied to other users. For Generic User id as a work around , I have created alias in .bashrc file and other user... (4 Replies)
Discussion started by: Amit Joshi
4 Replies

2. Shell Programming and Scripting

Read user input, Encrypt the data and write to file

Hi, can some one help me how to encrypt and decrypt a file. AIM: reade user input, encrypt it and save it to file. while decryption read the encrypted file decrypt it and save the output in some variable. Example: consider we have Credentials.txt file with content username: password... (5 Replies)
Discussion started by: saichand1985
5 Replies

3. Shell Programming and Scripting

Help with Bash piped while-read and a read user input at the same time

Hi I am new to writing script and want to use a Bash Piped while-read and read from user input. if something happens on server.log then do while loop or if something happend on user input then do while loop. Pseudocode something like: tail -n 3 -f server.log | while read serverline || read... (8 Replies)
Discussion started by: MyMorris
8 Replies

4. Programming

Converting a user inputted date to epoch time

Hi all , I need to know how to convert a time stamp entered by the user to be converted to GMT/UTC(epoch time) using mktime() and gmtime() for exapample the input will be put in the form ptm.tm_sec = 0; ptm.tm_min = 59; ptm.tm_hour = 11; ptm.tm_mday = 20;... (2 Replies)
Discussion started by: ada
2 Replies

5. Homework & Coursework Questions

Need User Inputted Date

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I'm making my first program with perl and am supposed to allow the user to either use the current date or enter a... (0 Replies)
Discussion started by: Zamereon
0 Replies

6. Shell Programming and Scripting

Shell script to read lines in a text file and filter user data

hi all, I have this file with some user data. example: $cat myfile.txt FName|LName|Gender|Company|Branch|Bday|Salary|Age aaaa|bbbb|male|cccc|dddd|19900814|15000|20| eeee|asdg|male|gggg|ksgu|19911216||| aara|bdbm|male|kkkk|acke|19931018||23| asad|kfjg|male|kkkc|gkgg|19921213|14000|24|... (4 Replies)
Discussion started by: srimal
4 Replies

7. Shell Programming and Scripting

Substituting a user Inputted word into a command

I am creating a menu driven system and i want to show the last login times of different users, instead of using the 'last' command i wanted to know if there is anyway i could make a brief search tool where the user can input which user they are looking for and then the login times for that specific... (2 Replies)
Discussion started by: warlock129
2 Replies

8. Programming

Not able to see the inputted command on the shell prompt..but its executing the comnd

Hi , I m writing a program which involves piping(pipes). In my program, once i execute the child process (dealing with pipe),I m not able to see any inputted command on the screen....but the entered command is getting executed... Actaully inorder to implement piping i hav closed STDIN and... (3 Replies)
Discussion started by: Crab
3 Replies

9. Shell Programming and Scripting

How do I compare a filesize to an inputted number?

I need to create a shell script that takes 2 arguments, the first argument is any number, and the second argument is a filename. The script needs to indicate if the file's size is bigger or smaller than the number provided. I know it will be some kind of if/then, but I can't figure out how to... (1 Reply)
Discussion started by: Modoc
1 Replies

10. Shell Programming and Scripting

How to hide user inputted text for interactive unix shell script?

Hi everybody, Do you know how to hide the text for interactive unix shell script? Just like the case for inputting password during logon. Patrick (1 Reply)
Discussion started by: patrickpang
1 Replies
Login or Register to Ask a Question