shell script looping [duplicate] - linux

This question already has answers here:
How to loop through dates using Bash?
(10 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 11 months ago.
I am new to bash scripting and trying to write a loop to run a curl command but however getting errors. Please suggest the write syntax.
#!/usr/bin/env sh
start = $1
end = $2
start=$(date -d $start +%Y-%m-%d)
end=$(date -d $end +%Y-%m-%d)
while [[ $start -le $end ]]
do
echo $start
curl -H "command, which can't be shared"
start=$(date -d"$start + 1 day" +"%Y-%m-%d")
done
The command line to run the script used sh filename.sh "2022-02-01" "2022-02-25".
However, the first error I received is windows cannot find '2022-02-01'. Make sure you typed the name correctly, and then try again.
Please suggest the errors.

Related

How to validate user input on a bash script? [duplicate]

This question already has answers here:
Validate the number of arguments passed in bash from read
(2 answers)
Closed 2 years ago.
I'm creating a bash script. However, I need the script to validate that the user has to enter two arguments. Not 1 and nothing more than 3.
echo -n "Enter two values"
read val1 val2
try this:
#!/bin/bash
if [ $# -eq 2 ]; then
echo "var1: $1 var2: $2"
else
echo "error: need to set two argument"
fi
run:
./scriptName.sh first second

I am trying to write a shell script to read username from a file, but it is not working. I am posting script I am writing and output [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
#!/bin/bash
if [ -f "$1" ]
then
for users in 'cat $1'
do
useradd $users
done
else
echo "input is not a file"
fi
You just have to get the input for the do loop right:
#!/bin/bash
if [ -f "$1" ]
then
for user in $(cat "$1")
do
useradd "$user"
done
else
echo "input is not a file"
fi
Remarks: this works for reading out a file word-by-word and I tried to keep your structure.
For reading out files line by line this is an elegant way: https://stackoverflow.com/a/4642213/2819581

get " [: if-then: unexpected operator ",when searching all sub-folder in a folder [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 4 years ago.
when I run this script in my Ubuntu:
for item in *
do
if [ -d $item ]
then
echo $item
fi
done
I got this output:
output
I don't understand why I get "[: if-then: unexpected operator"
Escape your variables. Read about it here.
for item in *
do
if [ -d "$item" ]
then
echo "$item"
fi
done

bash script - can't get for loop working

Background Info:
I'm trying to follow the example posted here: http://www.cyberciti.biz/faq/bash-for-loop/
I would like loop 9 times using a control variable called "i".
Problem Description
My code looks like this:
for i in {0..8..1}
do
echo "i is $i"
tmpdate=$(date -d "$i days" "+%b %d")
echo $tmpdate
done
When I run this code, the debug prints show me:
"i is {0..8..1}"
instead of being a value between 0 and 8.
What I've Checked So Far:
I've tried to check my version of bash to make sure it supports this type of syntax. I'm running version 4,2,25(1)
I also tried using C like syntax where you do for (i=0;i<=8;i++) but that doesn't work either.
Any suggestions would be appreciated.
Thanks.
EDIT 1
I've also tried the following code:
for i in {0..8};
do
echo "i is $i"
tmpdate=$(date -d "$i days" "+%b %d")
echo $tmpdate
done
And...
for i in {0..8}
do
echo "i is $i"
tmpdate=$(date -d "$i days" "+%b %d")
echo $tmpdate
done
They all fail with the same results.
I also tried:
#!/bin/bash
for ((i=0;i<9;i++));
do
echo "i is $i"
tmpdate=$(date -d "$i days" "+%b %d")
echo $tmpdate
done
And that gives me the error:
test.sh: 4: test.sh: Syntax error: Bad for loop variable
FYI. I'm running on ubuntu 12
EDIT 2
Ok... so i think Weberick tipped me off to the issue...
To execute the script, I was running "sh test.sh"
when in the code I had defined it as a BASH script! My bad!
But here's the thing. Ultimately, I need it to work in both bash and sh.
so now that I'm being careful to make sure that I invoke the script the right way... I've noticed the following results:
when defined as a bash script and i execute using bash, the C-style version works!
when defined as an sh script and i execute using sh, the C-style version fails
me#devbox:~/tmp/test$ sh test.sh
test.sh: 5: test.sh: Syntax error: Bad for loop variable
when defined as an sh script and i execute using sh the NON c style version ( aka for i in {n ..x}), I get the "i is {0..8}" output.
PS. The ";" doesn't make a difference if you have the do on the next line...just FYI.
Ubuntu's default shell is dash, which doesn't recognise either of the bashisms (brace expansion, C-style for loop) you tried. Try running your script using bash explicitly:
bash myscript.sh
or by setting the shebang to #!/bin/bash. Make sure NOT to run the script with sh myscript.sh.
dash should work if you use seq:
for i in $(seq 0 1 8); do
echo "$i"
done
Just {0..8} should work in bash, the default increment is 1. If you want to use a C-style for loop in bash:
for ((i=0;i<9;i++)); do
echo "$i"
done
I'm confident that
#!/bin/bash
for ((i=0;i<9;i++))
do
echo "i is $i"
tmpdate=$(date -d "$i days" "+%b %d")
echo $tmpdate
done
work on Ubuntu 12.04
If you still have an error, can you please try running
chmod +x test.sh
then
./test.sh
And the result is
i is 0
Apr 04
i is 1
Apr 05
i is 2
Apr 06
i is 3
Apr 07
i is 4
Apr 08
i is 5
Apr 09
i is 6
Apr 10
i is 7
Apr 11
i is 8
Apr 12
I'm no expert at bash but according to tdlp you need a ; after the for statement. There are many ways to to a range. This is one of them.
#!/bin/bash
for i in `seq 1 8`; do
echo $i
done
The site you quote says
Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax:
So you can just use {0..8..1} when you have a bash version greater than 4.0, which I guess is not the case (try bash --version in your terminal). Instead of {0..8..1} you can also use {0..8}.
If you have an older version you can use instead of {START..END..INCREMENT} the command $(seq START INCREMENT END) in the for loop.

How to use curl in a shell script? [duplicate]

This question already has answers here:
Execute bash script from URL
(16 answers)
Difference between sh and Bash
(11 answers)
Closed 1 year ago.
I'm trying to run this shell script in order to install RVM in an Ubuntu box
#!/bin/bash
RVMHTTP="https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer"
CURLARGS="-f -s -S -k"
bash < <(curl $CURLARGS $RVMHTTP)
but I get the following error
Syntax error: Redirection unexpected
Also tested not using the variables, but same result, could you tell what I'm missing?
#!/bin/bash
CURL='/usr/bin/curl'
RVMHTTP="https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer"
CURLARGS="-f -s -S -k"
# you can store the result in a variable
raw="$($CURL $CURLARGS $RVMHTTP)"
# or you can redirect it into a file:
$CURL $CURLARGS $RVMHTTP > /tmp/rvm-installer
or:
Execute bash script from URL
url=”http://shahkrunalm.wordpress.com“
content=”$(curl -sLI “$url” | grep HTTP/1.1 | tail -1 | awk {‘print $2′})”
if [ ! -z $content ] && [ $content -eq 200 ]
then
echo “valid url”
else
echo “invalid url”
fi
Firstly, your example is looking quite correct and works well on my machine. You may go another way.
curl $CURLARGS $RVMHTTP > ./install.sh
All output now storing in ./install.sh file, which you can edit and execute.

Resources