Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need somthing like : user=[ "jack" "david" "root"] then check if `$1="david" do something.
Here is a quick and easy way to implement want you want in bash
declare -a USERS=("jack" "david" "root")
for i in "${USERS[#]}"
do
echo "current array entry: $i"
if [ "$i" == "$1" ]; then
echo "Found a match"
# do something
fi
done
Hope that helped
Since you are also gave the zsh tag, here is a zsh solution:
user=( jack david root )
if (( ${user[(Ie)$1]} > 0 ))
then
# $1 is in the user list
fi
${user[(Ie)$1]} calculates the position of $1 in the user array, and results in 0 if the user is not present.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I have the following list of file names (strings) in bash
"foo_file1.yaml" "file2.yaml" "anotherfile.yaml"
I want to filter out files matching the pattern foo*.yaml
The following test does not seem to work:
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
if [[ $file == foo*.yaml ]]; then
echo $file
fi
done
steps.changed-files.outputs.all_changed_files contains the aforementioned list.
(it is from a step in a GHA job but I don't think this is relevant)
Any suggestions?
edit: I have also tried this
echo $file | grep -e 'foo*\.yaml'
still doesn't work.
If you are using grep, you don't need to actually check the condition. Something like this should work:
#!/bin/bash
for file in "foo_file1.yaml" "file2.yaml" "anotherfile.yaml"; do
echo $file | grep -o 'foo.*.yaml'
done
It outputs:
foo_file1.yaml
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I hope someone can help me. my question is with a script in bash that receives a certain amount of parameters and then show them in the reverse order of the one. as I keep name one for each line in the vvariable and then show them around.
#!/bin/bash
var=""
for i in "$#";do
var+=`echo $i`
done
If you want reversed order, you have to loop in reversed order:
for ((i=$#; i>=1;i--)); do
a=${!i}
echo "$a"
done
The simplest way to achieve this would be:
echo $# | rev
$# stores all the arguments passed, and rev, as its name suggests, reverses the order of characters in the line.
Edit:
After reading your comment, I can suggest the following approach:
for i in `echo $# | rev`; do
j=`echo $i | rev`
echo -n "$j "
done
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How could I create a shell script with input data ?
Please enter names separated by a blank: John Marry Sanford Saunders
read names
AFAI understand you want to use an array:
read -r -a names
Example:
read -r -a names <<< "John Marry Sanford Saunders"
echo "${names[0]}" # John
echo "${names[1]}" # Marry
echo "${names[2]}" # Sanford
echo "${names[3]}" # Saunders
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What I'm wondering is this. I get barometric readings that I want to create an alert for. I have a working one, but I would like to add a time comparison to be more accurate.
Example would be if "X" number falls below "X" number in lets say 5 minutes then do something.
Thanks for listening.
You could do something like below :
#!/bin/bash
while [ 0 ]
do
oldval=`cat reading` # Suppose that reading is the file where readings are updated
sleep 5m # sleeps for 5 minutes
newval=`cat reading`
if (( $newval < $oldval-5 ))
then
echo "$( date ) : Beep Beep " | tee -a barrolog
#above steps prints the output as well as append it to a log file
else
echo "$( date ) : No change " | tee -a barrolog
fi
done
# The script never reaches this point.
The script checks the reading relative to the previous one. You may wish to compare the readings against fixed value.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have file with positive and negative values.
Want to export variable if everything is different from STATE_ACTIVE to be w1=STATE_INACTIVE if everything is STATE_ACTIVE to be w1=STATE_ACTIVE
STATE_ACTIVE
STATE_ACTIVE
STATE_INACTIVE
STATE_ACTIVE
I try with if operators but cannot able to catch the whole file values.
grep has a useful feature - $? is set based on whether it found anything or not.
grep -vq 'STATE_ACTIVE' myfile
if [ $? -eq 0 ]
then
export w1=STATE_INACTIVE
else
export w1=STATE_ACTIVE
fi
just grep -q 'STATE_INACTIVE' then read return code: $? if not zero, it means no INACTIVE found. otherwise, INACTIVE was hit.
-q makes grep don't output things.