I am developing a script in bash. This script receives a parameter by pipe and passes it to the script.
The problem is that the script is receiving null values.
# curl -s -d 'PATH_ROOT="/opt"' http://dominio.com/setup_cmi/install_cmi.sh |bash
$PATH_ROOT esta vacia, debe indicar el directorio raiz
#!/bin/bash
#Dependencias
declare -a dependencias=( "unzip" "wget" "curl" "mkdir" "tree" )
#command -V unzip
echo "Content-Type: text/plain"
echo
PATH_ROOT=$1
if [ -z "$PATH_ROOT" ]; then
echo "\$PATH_ROOT esta vacia, debe indicar el directorio raiz"
exit;
else
for i in "${dependencias[#]}"; do
command -V $i
if [ $? = 1 ]; then
exit;
fi
done
fi
Also try making the call in this way, with the same result:
curl -s http://dominio.com/setup_cmi/install_cmi.sh | bash -s -- /opt
Related
I am trying to build a script/program that runs a sh file on internet without downloading it. All was working fine, with the exception that i am getting "This file/command doesnt exist" error on gedit, apt, etc. Then, I tried to modify the script to download the file and execute it after this instead of running bash <(curl -s --max-redirs 10 $2 -L), but still not working. I realized that if I finalize the script, CD to the downloaded SH file path and run it using "bash + foo.sh", it is still not working. The only method I found to run it is editing the SH file using gedit, copy all the content, remove the SH file, create a new SH file, paste and run it.
What is happening? Why is it not working? I want to run a SH script on the web just with bash without downloading it (bash and curl).
Here is my script (I will send it to my github when I end):
#!/bin/bash
input=$*
mkdir "$HOME/.br" > /dev/null 2>&1
mkdir "$HOME/.br/cache" > /dev/null 2>&1
rm "$HOME/.br/cache/*" > /dev/null 2>&1
if [ "$input" == "" ]
then
echo '[X] ERRO! | ERROR! | ¡ERROR!'
echo
echo '[PT]: Você precisa digitar um argumento. Digite "br -- help" para obter ajuda.'
echo
echo '[EN]: You need to type an argument. Type "br --help" to get help.'
echo
echo '[ES]: Debes ingresar un argumento. Escriba "br --help" para obtener ayuda.'
exit
fi
if [ "$input" == "--ajuda" ] || [ "$input" == "--help" ] || [ "$input" == "-h" ] || [ "$input" == "--ayuda" ] || [ "$input" == "-?" ]
then
echo '[?] AJUDA | HELP | AYUDA'
echo
echo '[PT]: Acesse este link para obter ajuda: '
echo
echo '[EN]: Go to this link for help: '
echo
echo '[ES]: Vaya a este link para obtener ayuda: '
exit
fi
if [ "$1" == "-u" ] || [ "$1" == "--url" ]
then
curl -s --max-redirs 10 $2 -L >$HOME/.br/cache/sh.sh
chmod +x "$HOME/.br/cache/sh.sh"
bash "$HOME/.br/cache/sh.sh" $1 $2 $3 $4 $5 $6 $7 $8 $9
else
curl -s --max-redirs 10 tiny.cc/brl_$1 -L >$HOME/.br/cache/sh.sh
chmod +x "$HOME/.br/cache/sh.sh" $1 $2 $3 $4 $5 $6 $7 $8 $9
fi
Also:
PS: I know that downloading files without reading the content before is unsafe, and because of it I pretend to make a dialog that shows its content, if the user wants it.
PS2: You can see that line: curl -s --max-redirs 10 tiny.cc/brl_$1 -L >$HOME/.br/cache/sh.sh. I am making a functionality that you can just short a link using tiny.cc with "brl_filename" and acess it typing "./script.sh filename" (That acess the SH file stored on 'tiny.cc/brl_filename'). It's like APT-GET, but its a SH script 'reader', and i'm using the tiny.cc as a 'database'. Imagine some website saying: "To install my app, just type foo filename or click in This button (foo://filename), and all twill be done. If you want to view what is inside the file, type foo -v filename or click in This button (foo://v:filename)!" - It's exactly it I'm trying to do, simplify the linux installing for all users, including the noobie users.
This project will be hosted on https://github.com/Felipecconde/br.
I have to verify in a script bash that a folder exists. The problem is, it does but the script tells me that my file does not exist.
I don't find the error and I have searched on the internet and I do exactly the way it says to do( on Openclassroom, forum, ...).
here is my script:
#!/bin/bash
if [ $# -ne 1 ] && [ -d $1 ]; then
echo "Usage : $0 dir">/dev/stderr
exit 1
fi
backup="~/backup"
echo $backup , argument = $1
if [ ! -e ${backup} ]; then
echo "Le dossier backup n'existe pas">/dev/stderr
exit 1
fi
if [ ! -d $backup ]; then
echo "le document backup n'est pas un fichier">/dev/stderr
exit 1
fi
if [ -w $backup ]; then
echo "Le dossier backup n'est pas protégé en écriture">/dev/stderr
exit 1
fi
il qu$if [! chmod u=+w $backup ]; then
echo "une erreur c'est produite">/dev/stderr
exit 1
fi
And what it said in the shell + the proof that the file backup exist in the correct repertory:
guy#PC-DE-GUY:~/bash/Chap9$ ./backup.sh ~/seance06/
~/backup , argument = /home/guy/seance06/
Le dossier backup n'existe pas
guy#PC-DE-GUY:~/bash/Chap9$ ~/backup
bash: /home/guy/backup : est un dossier
guy#PC-DE-GUY:~/bash/Chap9$ cd ~/backup
guy#PC-DE-GUY:~/backup$
The issue is with the ~ character inside quotes, in backup="~/backup" (line 6). BASH processes it as a character, not as an alias to your home directory, so your code runs the checks on the string '~/backup', instead of '/home/guy/backup'.
To use ~ as an alias to your home directory, it should be outside quotes: backup=~/backup
If you want to hardcode your home directory, I suggest you use $HOME instead of ~. It's clearer to understand.
You can test what I tried to explain with the following code:
#!/bin/bash
# Save this file as test.sh
# run this script with ~ as an argument:
# ./test.sh ~
echo "Value for \$HOME: $HOME"
echo "Value for ~ inside quotes: ~"
echo "Value for ~ outside quotes:" ~
echo "Value for \$1: $1"
If you save the code above as test.sh and you run it as: ./test.sh ~, outputs will be:
Value for $HOME: /home/guy
Value for ~ inside quotes: ~
Value for ~ outside quotes: /home/guy
Value for $1: /home/guy
I have a shell Unix running every hour (crontab on CentOS 7).
Inside that shell, a loop read and proceed treatment for all new files find in a defined folder.
At the end of each files's treatment a CURL command is send with some parameters, for example :
curl https://aaaaaa.com/website -d param1=value1 -d param2=value2 ....
Each time the shell is run by crontab, the 1st CURL is correctly converted to a true URL and received by Apache/Tomcat, but all the others are bad. In fact the 2nd and the following CURLs seem not converted in the correct format like
https://aaaaaa.com/website?param1=value1¶m2=value2
but they are sent like
https://aaaaaa.com/website -d param1=value1 -d param2=value2
So the website is unable to treat the parameters properly.
Why the 1st command is correctly converted to a correct URL format and not the following ?
EDIT - EDIT
The part of shell :
#!/bin/bash
...
#======================================================
# FUNCTIONS
#======================================================
UpdateStatus () {
CMD_CURL="${URL_WEBSITE} -d client=CLIENT -d site=TEST -d produit=MEDIASFILES -d action=update"
CMD_CURL="${CMD_CURL} -d codecmd=UPDATE_MEDIA_STATUS"
CMD_CURL="${CMD_CURL} -d idmedia=$4"
CMD_CURL="${CMD_CURL} -d idbatch=$3"
CMD_CURL="${CMD_CURL} -d statusmedia=$2"
if [[ ! -z "$5" ]]; then
CMD_CURL="${CMD_CURL} -d filename=$5"
fi
echo " ${CMD_CURL}" >> $1
CURL_RESULT=`curl -k ${CMD_CURL}`
CURL_RESULT=`echo ${CURL_RESULT} | tr -d ' '`
echo " Result CURL = ${CURL_RESULT}" >> $1
if [ "${CURL_RESULT}" = "OK" ]; then
return 0
fi
return 1
}
#======================================================
# MAIN PROGRAM
#======================================================
echo "----- Batch in progress : `date '+%d/%m/%y - %H:%M:%S'` -----"
for file in $( ls ${DIR_FACTORY_BATCHFILES}/*.batch )
do
...
old_IFS=$IFS
while IFS=';' read <&3 F_STATUS F_FILEIN F_TYPE F_CODE F_ID F_IDPARENT F_TAGID3 F_PROF F_YEARMEDIA F_DATECOURS F_TIMEBEGINCOURS F_LANG || [[ -n "$F_STATUS $F_FILEIN $F_TYPE $F_CODE $F_ID $F_IDPARENT $F_TAGID3 $F_PROF $F_YEARMEDIA $F_DATECOURS $F_TIMEBEGINCOURS $F_LANG" && $F_STATUS ]];
do
...
UpdateStatus ${LOG_FILENAME} ${STATUS_ERROR} ${F_ID} ${F_IDPARENT}
...
done 3< $file
IFS=$Old_IFS
...
done
You need to provide the "-d" flags and values before the URL so:
curl -d param1=value1 -d param2=value2 https://aaaaaa.com/website
Moreover, this command is going to send the parameters/values as POST parameters, not query parameters. You can use the "-G" flag, possibly combined with "--url-encode" to send as query parameters, see:
https://unix.stackexchange.com/questions/86729/any-way-to-encode-the-url-in-curl-command
I need to create a number of folders with the same name but with a number at the end.
User should write a number and the script should create these numbers of folders. I don't know how to link the number and the number os folders.
Here is my script
#!/bin/bash
echo "(1617S2)" A.C.B.S
pwd
date
NOM="A.C.B.S"
echo $NOM
echo -n "Introduce el numero de carpetas que quieras :"
read x
if (($x<=10)); then
echo "Son $x carpetas"
else (($ <10))
echo -n "El número de carpetas tiene que ser entre 1 i 10 :"
read x2
echo "Son $x2 carpetas"
fi
cd ..
cd /var
sudo mkdir carpetaprincipal
cd ..
cd /var/carpetaprincipal
sudo mkdir carpeta {1..10}
You could use seq with xargs to iterate and create the number of folders chosen for the input:
#!/bin/bash
echo "(1617S2)" A.C.B.S
pwd ; date
NOM="A.C.B.S" ; echo $NOM
function makeFolders () {
echo -n "Introduce el numero de carpetas que quieras :"
read -r x
if [[ "$x" -lt 11 ]] && [[ "$x" -gt 0 ]]; then
echo "Son $x carpetas"
cd ../var || exit
mkdir carpetaprincipal
cd carpetaprincipal || exit
seq "$x" | xargs -I{} mkdir carpeta_{}
fi
if [[ "$x" -lt 1 ]] || [[ "$x" -gt 10 ]]; then
echo "El número de carpetas tiene que ser entre 1 i 10!"
makeFolders # you could optionally exit 1 here
fi
}
makeFolders
There were some other issues with your script, mainly the logic didn't make sense. If you put in more than 10 or less than 1 then the script still allowed the user to create folders which are above are below what is supposed to be allowed. Putting those methods within in a function also allows you to return to the input line if there is an error. Also, read should include -r, otherwise it has the potential to mangle backslashes.
To do multiple mkdir while looping a variable number of times:
x2=4
i=1
while [ "$i" -le "$x2" ]
do
sudo mkdir carpeta$1
i=$(($i + 1))
done
I have a webservice provided at http://localhost/test/testweb
I want to write a script to check if webservice is up with curl
If there a curl parameter given, returns 200 OK ok true false so that I can use it is if-else block in linux script
curl -sL -w "%{http_code}\\n" "http://www.google.com/" -o /dev/null
-s = Silent cURL's output
-L = Follow redirects
-w = Custom output format
-o = Redirects the HTML output to /dev/null
Example:
[~]$ curl -sL -w "%{http_code}\\n" "http://www.google.com/" -o /dev/null
200
I would probably remove the \\n if I were to capture the output.
I use:
curl -f -s -I "http://example.com" &>/dev/null && echo OK || echo FAIL
-f --fail Fail silently (no output at all) on HTTP errors
-s --silent Silent mode
-I --head Show document info only
Note:
depending on needs you can also remove the "-I" because in some cases you need to do a GET and not a HEAD
Same as #burhan-khalid, but added --connect-timeout 3 and --max-time 5.
test_command='curl -sL \
-w "%{http_code}\\n" \
"http://www.google.com:8080/" \
-o /dev/null \
--connect-timeout 3 \
--max-time 5'
if [ $(test_command) == "200" ] ;
then
echo "OK" ;
else
echo "KO" ;
fi
That will check the headers via wget 2>&1pipes the stderr to stdout
grep filters
-O /dev/null just throws the content of the page
if [ "\`wget http://example.org/ -O /dev/null -S --quiet 2>&1 | grep '200 OK'\`" != "" ];
then
echo Hello;
fi;
I know not curl, but still a solution
I needed a better answer to this, so I wrote the script below.
The fakePhrase is used to detect ISP "Search Assist" adware HTTP resposnes.
#!/bin/bash
fakePhrase="verizon"
siteList=(
'http://google.com'
'https://google.com'
'http://wikipedia.org'
'https://wikipedia.org'
'http://cantgettherefromhere'
'http://searchassist.verizon.com'
)
exitStatus=0
function isUp {
http=`curl -sL -w "%{http_code}" "$1" -o temp_isUp`
fakeResponse=`cat temp_isUp | grep $fakePhrase`
if [ -n "$fakeResponse" ]; then
http=$fakePhrase
fi
case $http in
[2]*)
;;
[3]*)
echo 'Redirect'
;;
[4]*)
exitStatus=4
echo "$1 is DENIED with ${http}"
;;
[5]*)
exitStatus=5
echo "$1 is ERROR with ${http}"
;;
*)
exitStatus=6
echo "$1 is NO RESPONSE with ${http}"
;;
esac
}
for var in "${siteList[#]}"
do
isUp $var
done
if [ "$exitStatus" -eq "0" ]; then
echo 'All up'
fi
rm temp_isUp
exit $exitStatus
Use this:
curl -o $CURL_OUTPUT -s -w %{http_code}\\n%{time_total}\\n $URL > $TMP_FILE 2>&1
cat $TMP_FILE