How-To Trim user input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How-To Trim user input
# 1  
Old 02-25-2013
Wrench How-To Trim user input

Hi,

The user inputs either "/tmp/bea" or "/tmp/bea/". Regardless my script should extract and store in a variable the last directory in the user input path.

Desired output: "bea"

Please help.
# 2  
Old 02-25-2013
Code:
var=$(basename /tmp/bea/)

This User Gave Thanks to Subbeh For This Post:
# 3  
Old 02-25-2013
May be you can try parameter expansion.

Shell Command Language
# 4  
Old 02-25-2013
Quote:
Originally Posted by Subbeh
Code:
var=$(basename /tmp/bea/)


Does basename command work across all platforms ?
# 5  
Old 02-26-2013
Question

Quote:
Originally Posted by Subbeh
Code:
var=$(basename /tmp/bea/)


When the user input is "/tmp"

I get the below error

Quote:
basename/tmp: not found.
# 6  
Old 02-26-2013
Code:
basename <directorywhich has complete path>
 
eg:(solaris 9)
$ basename /tmp
tmp

# 7  
Old 02-26-2013
It worked OK for me:

Code:
$ basename /tmp
tmp

Which unix version are you in?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

User input for shell

I still cannot figure out how to get this read command to work. I want the script to ask questions when prompted, and the user to enter a response. Based on response it will continue or exit. I have not worked with this type of script before so I am almost clueless on what to do, and so far google... (6 Replies)
Discussion started by: bigbenn
6 Replies

3. Shell Programming and Scripting

Check user input

Hi, I need my script to check if the user enters 3 values if not 5 values to my script and alert if the input has any other number of values. for example: ./myscript.sh 22 56 3221 - > correct ./myscript.sh 22 56 3221 45 777 -> correct ./myscript.sh 22 56 3221 45 -> incorrect Please... (6 Replies)
Discussion started by: mohtashims
6 Replies

4. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

5. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

6. Shell Programming and Scripting

user input in perl?

Please tell me how to write a perl script that asks the user to enter words and that passes them to a variable. In bash, the "read" command would achieve such user interaction. #!/bin/bash read -p "Enter files: " vFiles However, I am looking for perl version of something equivalent... (2 Replies)
Discussion started by: LessNux
2 Replies

7. Shell Programming and Scripting

Change ip from user input

I have 2 Servers IP: 1)10.1.1.27 2)10.1.2.27 #more /etc/postfix/transport Domain.com smtp:ServerIP Now i want to change the smtp:ServerIP according to user interaction: (2 Replies)
Discussion started by: telnor
2 Replies

8. UNIX for Dummies Questions & Answers

accept user input?

how would i accept user input from the keyboard? (2 Replies)
Discussion started by: JamieMurry
2 Replies

9. Shell Programming and Scripting

awk user input

Using the following I'm trying to print the user's response to the prompt Y / N but I get nothing other than the contents of $1? awk '{ printf($1 " ? (Y/N)") getline myresponse < "-" system("read myresponse") if (myresponse == "Y") { print $1... (17 Replies)
Discussion started by: gefa
17 Replies

10. Shell Programming and Scripting

Getting user input

I am trying to create a shell (ksh) which has two "read" commands, one which reads a line from a file and another which is inside a loop that reads user input from a keyboard. However, the "read" command inside the loop uses the input from the file and it does not get the user input from keyboard.... (3 Replies)
Discussion started by: stevefox
3 Replies
Login or Register to Ask a Question