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:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)