BASH - set specific user variable via string operators


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH - set specific user variable via string operators
# 1  
Old 09-24-2010
BASH - set specific user variable via string operators

Apologies for the utter triviality of this question, but we all have to start somewhere! I've also tried searching but this question is pretty vague so I didn't (a) really know what to search for or (b) get many relevant hits to what I did search for.

Anyway, I'm in the process of self-teaching basic (bash) scripting as I'm bound to need to know it eventually. Currently, I'm just reading/working through "Learning the bash shell" (Newham & Bosenblatt) and working through the exercises which, to be fair, aren't exactly difficult. However, I'm puzzled by one particular facet of one of the tasks, specifically 'string operators'....

The task is simple, recursively sort a text file and print the top X results. This is OK and makes perfect sense. I'm trying to take it one step further and add a user variable to optionally print a header. This is pretty easily accomplished via:

Code:
header=$1
file=$2
file=${file:?"input file not specified"}
number=$3

echo -e -n ${header:+"albums artist\n"}

sort -nr $file | head -${number:=10}


but this prints the headers if anything is in position $1. How would I go about only printing the header if the user defines "-h" in $1? It's a pretty trivial problem to be fair, but its been bugging me for a while now. Of the top of my head I'm guessing theres a way to have a variable only defined (i.e. $header in this case) when theres a particular string input (i.e. -h) in $1, but I'm not entirely sure on the syntax to accomplish it. Note that I'm aware I could probably do this with an if statement or something, but I'd like to do it via a string operator.

Cheers. Smilie
# 2  
Old 09-24-2010
With only 1 option, there's no need of getopts (which is a powerful feature of bash), so you can proceed this way:
bash code:
  1. if [ "$1" = "-h" ]
  2. then
  3.    header=$2
  4.    shift 2 # puts the 3d param in $1 and the 4th in $2
  5. fi
  6. file=${[B]1[/B]:?"input file not specified"}
  7. echo -e -n ${header:+"albums artist\n"}
  8. sort -nr $file | head -${2:=10}
I simplified it a bit, avoiding intermediate variable names.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance. let me be specific. here's my code: # URL port Variables port2195=`nc -z $url2195 2195` port2196=`nc -z $url2196 2196`... (5 Replies)
Discussion started by: hungryd
5 Replies

2. Shell Programming and Scripting

If a string variable value exists in a set of values

Can some one please help me with the syntax in shell script for the below : if $var1 exists in ('val1','val2','val3') I want to execute a set of commands if the value of var1 variable matches any one of the given string values. Please let me know if there are any other option to go by. ... (10 Replies)
Discussion started by: Pandee
10 Replies

3. Shell Programming and Scripting

Bash script set command to a variable

Hi, Will following set up work in bash script? I've got errors if assigning following binary command to a variable. But on the other hand, COMMAND="ls" works. Any explanation please? How can I assign binary command to a variable COMMAND then I can just call ${COMMAND}? COMMAND="rsync"... (3 Replies)
Discussion started by: hce
3 Replies

4. Shell Programming and Scripting

Bash - get specific character from the string

Hi! If I want to extract a character from a specific position of a string, I can use ${string:1:1} (if I want character at the position 1). How can I do the same thing, when the number of position is contained in the variable? ${string:$var:1}doesn't work, unfortunately. Thanks in advance. (2 Replies)
Discussion started by: xqwzts
2 Replies

5. Shell Programming and Scripting

bash variable (set via awk+sed) not working as expected

Hi! Been working on a script and I've been having a problem. I've finally narrowed it down to this variable I'm setting: servername=$(awk -v FS=\/ '{ print $7 } blah.txt | sed 's\/./-/g' | awk -v FS=\- '{print $1}')" This will essentially pare down a line like this: ... (7 Replies)
Discussion started by: creativedynamo
7 Replies

6. Shell Programming and Scripting

Howto set string variable in TCL?

Anyone knows how to set variable in TCL ? let say i have a password variable and it will have different values. set variable and variable has different values like: xxxx yyyy zzzz (0 Replies)
Discussion started by: linuxgeek
0 Replies

7. UNIX for Dummies Questions & Answers

set sub-string as variable

Hi all, i really new in linux and just heard about shell scripting couple days ago.. i did exercises on linux in online tutorial but as a beginner, i'm facing problems in developing the script as there are errors that sometimes i dun have any idea on how to solve it.What i'm doing now is not... (3 Replies)
Discussion started by: tedy2808
3 Replies

8. Shell Programming and Scripting

Set specific part in command output into variable

I am trying unsuccessfully to set into a variable a specific part of command output: The command output will be as: line 1: <varied> line 2: 2 options: option 1: Set view: ** NONE ** or option 2: Set view: <different_name_of_views_always_without_spaces> and I would like to get into... (7 Replies)
Discussion started by: orit
7 Replies

9. UNIX for Advanced & Expert Users

If with set operators

Hi guys, I'm trying to run more than one "if" condition at once. What I want is something like if ] or ] or ]; then ... I can't remember the syntax for using this or/and set operators. Can someone please assist/ jog my memory? thanks Khoom (2 Replies)
Discussion started by: Khoomfire
2 Replies

10. Shell Programming and Scripting

[bash] Check if variable is set or blank

Hello guys. In my script, i have the following code: echo "The tarfile contains these directorys" tar -tf file.tar > tarlist.txt cat tarlist | awk -F/ '{print $1 "/" $2}' | nl echo "Enter path of the directory you want to extract or just press enter to extract everything: " read path... (1 Reply)
Discussion started by: noratx
1 Replies
Login or Register to Ask a Question