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
Related
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 1 year ago.
Improve this question
I have a string, for example /sd1/sd2/sd3/
I want to write it to a file in the next way
/sd1
/sd1/sd2
/sd1/sd2/sd3
How could I do that?
Note: the length of the string could be different. i.e. /sd1/ or /sd1/sd2/sd3/sd4/sd5/
This solution works for me
string="/sd1/sd2/sd3/"
IFS='/' read -r -a sds <<< "$string"
tmpsd=""
for (( i=1; i<${#sds[#]}; i++ )); do
tmpsd="${tmpsd}/${sds[i]}"
echo ${tmpsd} >> file.txt
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 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.
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 have some 50 files in D:\dummy\ folder. It has filenames like
Abc_566_1.xml.error
Abc_566_2.xml.error
...
Abc&566_50.xml.error
I want a shell script/solution to remove .error in all 50 filenames.
The shell can do quite an amount of string processing, like chopping off substrings at either end of a string. I usually do jobs like yours with
for file in *.error; do
mv "$file" "${file%.error}"
done
For more on this, read your shell manual, especially the section on parameter expansion.
Give this a try:
for each in *.error ; do
mv $each `echo $each | sed -e 's/\.error$//'`
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 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 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 9 years ago.
Improve this question
I want get a string which is between two characters in bash script. For example;
AAA_Ubuntu-bbb, in this string I want to get "Ubuntu" as string. How I do it?
var=AAA_Ubuntu-bbb
string=${var#*_} # remove before _
string=${string%%-*} # remove after -
echo "$string"
See the section of the bash man page on Parameter Expansion for explanations of these operators.
Assuming the portion of the string you want is enclosed between _ and - and does not contain such characters, cut provides an easy solution. Define a function like this:
function extractName {
cut -d _ -f 2 <<< "$1" | cut -d - -f 1
}
and use it in your code like:
name=$(extractName "AAA_Ubuntu-bbb")
echo $name