I use shunit2 to perform some unit tests on a shell script.
To be able to access the script's functions I source it
. script_file.sh --source-only
And in the script I have everything encapsulated in a function but for
if [ "${1}" != "--source-only" ]; then
main "${#}"
fi
This works on Linux and OS X. I have now set up Travis CI with the following .travis.yml
language: bash
before_script:
- curl -L "http://downloads.sourceforge.net/shunit2/shunit2-2.0.3.tgz" | tar zx
- chmod +x $(pwd)/shunit2-2.0.3/src/shell/shunit2
script:
- export SHUNIT2=$(pwd)/shunit2-2.0.3/src/shell/shunit2
- make test
On Travis CI I see
Using worker: worker-linux-docker-f8d37801.prod.travis-ci.org:travis-linux-1
[...]
git.checkout
0.38s$ git clone --depth=50 --branch=master https://github.com/matteocorti/check_ssl_cert.git matteocorti/check_ssl_cert
Cloning into 'matteocorti/check_ssl_cert'...
remote: Counting objects: 285, done.
remote: Compressing objects: 100% (97/97), done.
remote: Total 285 (delta 187), reused 268 (delta 170), pack-reused 0
Receiving objects: 100% (285/285), 191.59 KiB | 0 bytes/s, done.
Resolving deltas: 100% (187/187), done.
Checking connectivity... done.
$ cd matteocorti/check_ssl_cert
$ git checkout -qf 85cb898990e583d8eac14ec693dbd79d9f3e9e6b
This job is running on container-based infrastructure, which does not allow use of 'sudo', setuid and setguid executables.
If you require sudo, add 'sudo: required' to your .travis.yml
See http://docs.travis-ci.com/user/workers/container-based-infrastructure/ for details.
before_script.1
0.26s$ curl -L "http://downloads.sourceforge.net/shunit2/shunit2-2.0.3.tgz" | tar zx
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 418 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 385 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 34113 100 34113 0 0 133k 0 --:--:-- --:--:-- --:--:-- 133k
before_script.2
0.00s$ chmod +x $(pwd)/shunit2-2.0.3/src/shell/shunit2
0.00s$ export SHUNIT2=$(pwd)/shunit2-2.0.3/src/shell/shunit2
The command "export SHUNIT2=$(pwd)/shunit2-2.0.3/src/shell/shunit2" exited with 0.
0.02s$ make test
( cd test && ./unit_tests.sh )
+[ -z /home/travis/build/matteocorti/check_ssl_cert/shunit2-2.0.3/src/shell/shunit2 ]
+[ ! -x /home/travis/build/matteocorti/check_ssl_cert/shunit2-2.0.3/src/shell/shunit2 ]
+SCRIPT=../check_ssl_cert
+[ ! -r ../check_ssl_cert ]
+NAGIOS_OK=0
+NAGIOS_CRITICAL=1
+NAGIOS_WARNING=2
+NAGIOS_UNKNOWN=3
+. ../check_ssl_cert --source-only
+VERSION=1.18.0
+SHORTNAME=SSL_CERT
+VALID_ATTRIBUTES=,startdate,enddate,subject,issuer,serial,modulus,serial,hash,email,ocsp_uri,fingerprint,
+[ != --source-only ]
+main
[...]
It seems that the script is called w/o parameters although a couple of lines above it clearly shows that it is not the case
+. ../check_ssl_cert --source-only
Am I missing something?
Travis is most likely using some shell other than bash for its /bin/sh interpreter and arguments to the ./source operator are not POSIX specified and that shell clearly doesn't support them.
Related
i have the following command to curl a list of urls but I want to rename the output files to something else, as the original name is really long and results in the error below:
command:
jq -r '… | .[]' | xargs -I{} curl -O {}
error:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0Warning: Failed to create the file
Warning: 270230751_944073806238863_7984852070388291566_n.jpg?stp=dst-jpg_e35&_n
Warning: c_ht=example.njrsnnkjnrjgngkrnngrggk&_nc_cat=111&_nc_ohc=Ch26T4U5kDIAX
Warning: 8viWsL&edm=AABBvjUBAAAA&ccb=7-4&ig_cache_key=MjczOTA2MDk4MDU3MTIyNjQ1N
0 92492 0 1 0 0 1 0 25:41:32 --:--:-- 25:41:32 1
curl: (23) Failed writing body (0 != 1)
how to pass some argument to curl so that it renames the file to a smaller name eg 16 digit uuid or something like njfnjsnf48u8 but not too common as there can be upto 1000 files being downloaded at once.
You can manipulate the {} variable that xargs uses in curl command to make it more unique. If you are sure that there are no duplicate urls in the input file then the following might work.
curl {} -o $(echo {}|base64)
I would like to create a server side git hook to perform a sanity check on a configuration file in a repository.
Based on my research I came across "pre-receive" hooks ( a hook that gets triggered upon push ).
I setup my testing atlassian bitbucket server and create a new repository to which I navigated into the pre-receive hook directory in which scripts should reside.
Created a test script that rejects any push on master with an error print. Everything works fine so far.
Now, to my sanity check hook.
I came up with the following.
#!/usr/bin/env bash
CONFIGFILE = "full-path-to-config-file"
EXPECTED_CONF_MASTER = "configEntry1=expectedValue1"
EXPECTED_CONF_TEST = "configEntry2=expectedValue2"
while read oldrev newrev refname; do
echo "$refname : $oldrev --> $newrev"
current_branch=$refname
short_current_branch="$(echo $current_branch | sed 's/refs\/heads\///g')"
newFiles=$(git diff --stat --name-only --diff-filter=ACMRT ${oldrev} ${newrev})
if [[ "$short_current_branch" = "test" ]]; then
echo $newFiles | grep $CONFIGFILE >/dev/null || exit 0 #CONFIGFILE has not been modified
git cat-file --textconv "${newrev}:$CONFIGFILE" | grep "$EXPECTED_CONF_TEST" || echo "invalid config" && exit 1
elif [[ "$short_current_branch" = "master" ]]; then
echo $newFiles | grep $CONFIGFILE >/dev/null || exit 0 #CONFIGFILE has not been modified
git cat-file --textconv "${newrev}:$CONFIGFILE" | grep "$EXPECTED_CONF_MASTER" || echo "invalid config" && exit 1
fi
done
However, I keep getting this error upon push. I am not sure what is wrong.
Username for 'http://localhost:7990': seif
Password for 'http://seif#localhost:7990':
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 386 bytes | 386.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: refs/heads/test : 1820f938babb12800d7d7726859bb86c03edb12a --> 28a1e8113d9ceebc41c6dd2d29a1ec2ebf626510
remote:
remote: fatal: Invalid revision range 1820f938babb12800d7d7726859bb86c03edb12a..28a1e8113d9ceebc41c6dd2d29a1ec2ebf626510
remote:
remote: Create pull request for test:
remote: http://localhost:7990/projects/myproj1/repos/bamboo-specs/pull-requests?create&sourceBranch=refs/heads/test
remote:
To http://localhost:7990/scm/myproj1/bamboo-specs.git
1820f93..28a1e81 test -> test
I am writing a small bash script that gets the profile of a Minecraft user from their username. However, when I run the following script, the profile_json variable comes out blank while the uuid_json and uuid variables have the content they should.
#!/bin/bash
# settings
username="Notch"
# script
uuid_json=$(curl https://api.mojang.com/users/profiles/minecraft/$username)
echo $uuid_json
uuid=$(jq '.id' <<< "$uuid_json")
echo $uuid
profile_json=$(curl https://sessionserver.mojang.com/session/minecraft/profile/$uuid)
echo $profile_json
Moreover, the output from running the script in a terminal shows the Current Speed of profile_json=$(curl https://sessionserver.mojang.com/session/minecraft/profile/$uuid) as 0. Any ideas as to why this would be?
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 56 100 56 0 0 160 0 --:--:-- --:--:-- --:--:-- 160
{"name":"Notch","id":"069a79f444e94726a5befca90e38aaf5"}
"069a79f444e94726a5befca90e38aaf5"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
AS you can see by executing echo $uuid you got "069a79f444e94726a5befca90e38aaf5"
instead of 069a79f444e94726a5befca90e38aaf5 there should be no quotes.
jq has option to remove them it's -r with that everything works as expected(I think).
So change it to:
#!/bin/bash
# settings
username="Notch"
# script
uuid_json=$(curl https://api.mojang.com/users/profiles/minecraft/$username)
echo $uuid_json
uuid=$(jq -r '.id' <<< "$uuid_json") echo $uuid
profile_json=$(curl https://sessionserver.mojang.com/session/minecraft/profile/$uuid)
echo $profile_json
Your echo $uuid outputs this:
"069a79f444e94726a5befca90e38aaf5"
But it really should be:
069a79f444e94726a5befca90e38aaf5
for the curl command to work. So, tweak your jq line to strip off quotation marks.
I am trying to make a bash script which basically takes a bunch of .debs, unpacks them and place binaries and libs in /usr/local/opt/{lib}bin.
The script checks whether / is mounted as ro or rw, and if mounted as ro to remount it as rw.
On chromebooks however, in order to mount / as rw you need to remove_rootfs_verification for the partition in question. The script fails to echo what stated above when rootfs_verification is enabled for /, and should exit 1, instead it carries on.
Here is the part of the script I am referring to
### ChromeOS's Specific!!!
# The following assumes rootfs_verification for / has already been removed
if grep $rootfs /proc/mounts | grep ro; then
mount -o remount,rw / &> mount.out
elif
grep -iw 'read-write' mount.out; then
echo '\nrootfs_verification for the root partition must to be removed in order to remount,rw /
To remove rootfs_verification run the following command and than reboot the system:
"sudo /usr/share/vboot/bin/make_dev_ssd.sh --remove_rootfs_verification --partitions 4"'
else exit 1
fi
The entire WIP script can be found here https://pastebin.com/ekEPSvYy
This is what happen when I execute it
localhost /usr/local # ./kvm_install.sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 124 0 124 0 0 322 0 --:--:-- --:--:-- --:--:-- 345
100 135 100 135 0 0 170 0 --:--:-- --:--:-- --:--:-- 170
100 60384 100 60384 0 0 57950 0 0:00:01 0:00:01 --:--:-- 344k
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 143 0 143 0 0 407 0 --:--:-- --:--:-- --:--:-- 412
100 154 100 154 0 0 202 0 --:--:-- --:--:-- --:--:-- 202
100 1298k 100 1298k 0 0 929k 0 0:00:01 0:00:01 --:--:-- 3020k
/dev/root / ext2 ro,seclabel,relatime,block_validity,barrier,user_xattr,acl 0 0
./kvm_install.sh: line 31: /etc/env.d/30kvm: Read-only file system
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 66802 100 66802 0 0 69657 0 --:--:-- --:--:-- --:--:-- 74555
./kvm_install.sh: line 39: ar: command not found
tar (child): control.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
md5sum: md5sums: No such file or directory
Basically what happens here is ar cannot be found as the script was unable to add the PATH variables to /etc/env.d/30kvm since the root partition cannot be mounted because roots_verification is enabled on /.
I tried adding the elif "grep" command in [[ as some suggested here, but that didn't work and adds further syntax issues.
I am in the process of learnign the basics of bash scripting. I apologize if the script is written poorly.
Thanks
I ultimately ended up doing this.
if grep $rootfs /proc/mounts | grep 'ro,'; then
mount -o remount,rw / &> mount.out
if
grep 'read-write' mount.out; then
echo 'something to echo' && exit 1
fi
fi
It is not pretty, but it works until I find/ learn a better way to implement the loop.
to make the output of a command a variable do:
$variable="$(command)"
if you want to use grep use the syntax:
command | grep text
if you want to do if statements use the syntax:
if [ some text ]; text
commands
elif [ some text ]
commands
else
commands
fi
for the some text check this cart
grep -iw is not valid on chromebook
rootfs has a different path depending on a lot of things if you want to save it as $rootfs do this command
rootfs="$(rootdev -s)"
also you made some mistake is the "|" and "&"
command1 | command to edit command1
command1 || run this command if command1 fails
command1 && run this command if command1 succeeds
Here's an example ip proxy checker shell script:
#!/bin/sh
while read IP
do
CURL=$(curl -x http://$IP -L http://icanhazip.com)
echo "$CURL"
done < ip.txt
But instead of a simple result like:
0.0.0.0
1.1.1.1
I get:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
0.0.0.0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
1.1.1.1
How can I quiet the extra stuff?
-s/--silent
Silent mode. Don't show progress meter or error messages. Makes Curl mute.
If this option is used twice, the second will again disable mute.
CURL=$(curl -s -x http://$IP -L http://icanhazip.com)